VB.net 跨线程

发布于 2024-08-21 05:05:22 字数 765 浏览 1 评论 0原文

我有一个需要执行的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-08-28 05:05:22

您只能从创建该窗口的线程访问(有一个例外)窗口(或与窗口相关的任何内容)的属性和方法。

但 WinForms 通过 Invoke(和 BeginInvoke)方法提供对此的支持,以通过 InvokeRequired 属性执行跨线程调用。

如果InvokeRequired为true,则需要使用Invoke来跨线程。为此,请包装您在其自己的方法中操作控件的代码,并直接调用或通过 Invoke 调用。 (要异步执行跨线程操作,请使用 BeginInvoke。)如下所示:

Private Sub IncrementProgress()
    teller += 1
    ProgressBar1.Value = teller

    If ProgressBar1.Value = ProgressBar1.Maximum Then
        TimerProgressbar.Stop()
    End If
End Sub      

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgressbar.Tick
  if ProgressBar1.RequiredInvoke Then
    ProgressBar1.Invoke(IncrementProgress)
  Else
    IncrementProgress
  End If
End Sub

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 (and BeginInvoke) methods to perform a cross thread call, with the InvokeRequired property.

If InvokeRequired is true, then you need to use Invoke to cross threads. To do this wrap your code that manipulates the control in its own method, and either call directly, or via Invoke. (To perform the operation cross threads asynchronously use BeginInvoke instead.) Something like:

Private Sub IncrementProgress()
    teller += 1
    ProgressBar1.Value = teller

    If ProgressBar1.Value = ProgressBar1.Maximum Then
        TimerProgressbar.Stop()
    End If
End Sub      

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerProgressbar.Tick
  if ProgressBar1.RequiredInvoke Then
    ProgressBar1.Invoke(IncrementProgress)
  Else
    IncrementProgress
  End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文