VB.net 跨线程
我有一个需要执行的cmd命令,当命令启动时它开始填充进度条。当cmd命令完成时,进度条需要填充到100。
这是我使用的代码,但是当progressbar.Value = 100出现时,它给了我一个错误。
Public Class Form1
Dim teller As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgressbar.Tick
teller += 1
ProgressBar1.Value = teller
If ProgressBar1.Value = ProgressBar1.Maximum Then
TimerProgressbar.Stop()
End If
End Sub
这是另一个私有子程序中的两个命令,其中应用程序崩溃了
ProgressBar1.Value = 100
TimerProgressbar.Stop()
当我调试它并尝试它时它崩溃了
ProgressBar1.Value = 100
但是当我在 Windows 7 下构建它时它运行良好而没有崩溃,但是有几个人报告我它崩溃了还有Windows xp系统。
VB 给了我关于 Cross Thread 的建议,但我不知道如何让它与此一起工作。
I have a cmd command that needs to be executed, when the command starts it starts to fill a progressbar. When the cmd command is done the progressbar needs to fill up to 100.
This is the code i use, but it gives me an error when the progressbar.Value = 100 comes up.
Public Class Form1
Dim teller As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgressbar.Tick
teller += 1
ProgressBar1.Value = teller
If ProgressBar1.Value = ProgressBar1.Maximum Then
TimerProgressbar.Stop()
End If
End Sub
This are the tow commands in another private sub where the app is crashing on
ProgressBar1.Value = 100
TimerProgressbar.Stop()
When i debug it and i try it out it crashes on
ProgressBar1.Value = 100
But when i build it under Windows 7 it runs fine without crashing, however a few people reported me it crashes on there Windows xp system.
VB gives me a suggestions about Cross Thread, but i don't know how i could make it work with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能从创建该窗口的线程访问(有一个例外)窗口(或与窗口相关的任何内容)的属性和方法。
但 WinForms 通过
Invoke
(和BeginInvoke
)方法提供对此的支持,以通过InvokeRequired
属性执行跨线程调用。如果
InvokeRequired
为true,则需要使用Invoke
来跨线程。为此,请包装您在其自己的方法中操作控件的代码,并直接调用或通过Invoke
调用。 (要异步执行跨线程操作,请使用BeginInvoke
。)如下所示:You can only access (with one exception) a Window's (or anything tied to Window) properties and methods from the thread that created that Window.
But WinForms includes support for this with the
Invoke
(andBeginInvoke
) methods to perform a cross thread call, with theInvokeRequired
property.If
InvokeRequired
is true, then you need to useInvoke
to cross threads. To do this wrap your code that manipulates the control in its own method, and either call directly, or viaInvoke
. (To perform the operation cross threads asynchronously useBeginInvoke
instead.) Something like: