辅助线程完成后,如何使主线程继续执行代码? [英] How to make main thread continue code after a secondary thread has completed?

查看:102
本文介绍了辅助线程完成后,如何使主线程继续执行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我想知道是否有可能在辅助线程完成时不使用sleep并让cicle等待值时使主线程继续.

我尝试激活处理程序,但是由处理程序执行的代码由辅助线程执行,并且可能发生跨线程操作的错误.

为了最大程度地减少这种错误和代码的发生,我现在有一个计时器,它每200毫秒检查一次这些变量的更改并启动相应的代码,但我希望找到一种可以避免使用cicle的方式.

我已经尝试过BackgroundWorkers,但是background.runworkercomplete也会给出跨线程操作的错误.

示例:我有一个子例程,我想检索一些值,并有一个处理程序,可在填充变量时在该值的标签上加上标签.

使用辅助线程或后台工作程序时,处理程序将由辅助线程或后台工作程序执行,并且始终会给我跨线程错误.

我在VB 2010中准备了一个带有文本框和两个按钮的表单的简单示例,这些按钮总是给我该错误.我要做的是由主线程更新文本框,而不接收错误.

示例:

Hello everyone!

I would like to know if it''s possible to make the main thread to continue when a secondary thread is finished without using sleep and while cicles to wait for a value.

I tryed activating a handler but the code executed by the handler is executed by the secondary thread and possible errors from cross thread operations can occurr.

For minimize that type of errors and code to prevent, i now have a timer that check those variables for change every 200ms and start the corresponding code but I whant to find a way that do not use CPU for cicles that can be avoided.

I have tryed BackgroundWorkers but the background.runworkercomplete will also give errors of cross thread operations.

Example: I have a sub routine that I want to retrieve some value and have a handler to put in a label that value when the variable are filled.

With a secondary thread or by a backgroundworker, the handler is executed by the secondary thread or by the backgroudworker and allways give me the cross thread error.

I prepared in VB 2010 a simple example of a form with a Textbox and two buttons that allways give me that error. What I want to do is to the textbox be updated by the main thread and not to receive the error.

Example:

Public Class Form1
      Private WithEvents aa As New testess
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
      End Sub
      Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            aa.str1 = "rthrghfghfg"
      End Sub
      Private Sub backgroundworkercomplete(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
            TextBox1.Text = aa.str1
      End Sub
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim thread As New System.Threading.Thread(AddressOf teste)
            thread.IsBackground = True
            thread.Start()
      End Sub
      Private Sub teste()
            aa.str1 = "rthrghfghfg"
      End Sub
      Private Sub testesfim(ByVal val As String) Handles aa.testes
            TextBox1.Text = aa.str1
      End Sub
End Class
Class testess
      Private str As String
      Public Event testes(ByVal val As String)
      Property str1 As String
            Get
                  str1 = str
            End Get
            Set(ByVal value As String)
                  str = value
                  RaiseEvent testes(str)
            End Set
      End Property
End Class



谢谢.

Flavio



Thank you.

Flavio

推荐答案

线程并行工作,因此,如果在主程序中创建并启动线程,则该线程将并行工作,并且主程序将在同时.

如果要让值形成该线程,则必须在完成后从该线程创建对主程序的回调.

如果您在用户界面中,请使用BackgroundWorker进行回调等.
Thread work in parallel, so if you create and start a thread in your main program, that thread will work in parallel and your main program will continue at the same time.

If your want the value form that thread then you have to create a call back from the thread to your main program when it is finished.

If you are in the UI then use the BackgroundWorker which will do callbacks etc.


这篇关于辅助线程完成后,如何使主线程继续执行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆