| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖
标题:[求助] 多线程
只看楼主 加入收藏
Magicalsoft
Rank: 1
等 级:新手上路
帖 子:30
专家分:0
注 册:2005-12-16
收藏
 问题点数:0 回复次数:1 
[求助] 多线程

先看段代码:非常典型的简单多线程。
Dim t1 As Thread
Dim t2 As Thread
Private Sub Process1()
Dim i As Integer = 1
Do While True
ProgressBar1.Value = i
i += 1
If i = 10000 Then
i = 1
End If
Loop
'为了让用户看清进度条的进度,将线程处理速度变慢
Thread.Sleep(2)
End Sub
Private Sub Process2()
Dim i As Integer = 1
Do While True
ProgressBar2.Value = i
i += 1
If i = 10000 Then
i = 1
End If
Loop
'为了让用户看清进度条的进度,将线程处理速度变慢
Thread.Sleep(2)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
t1 = New Thread(AddressOf Process1)
t1.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
t1.Abort()
End Sub
Private Sub
Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
t2 = New Thread(AddressOf Process2)
t2.Start()
End Sub
Private Sub
Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
t2.Abort()
End Sub


为什么这段代码在 VS2003里正确运行,在VS2005中就不对了呢?发生了以下错误:Cross-thread operation not valid: Control 'ProgressBar1' accessed from a thread other than the thread it was created on.

搜索更多相关主题的帖子: 线程 
2005-12-28 14:47
meiwang2008
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2006-1-8
收藏
得分:0 
怎么会出现错误呢?

我爱这里的会员,我将权利支持你们学习!
2006-01-08 21:41
快速回复:[求助] 多线程
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021919 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved