Force multi-threaded VB.NET class to display results on a single form

前端 未结 4 1810
走了就别回头了
走了就别回头了 2020-12-22 07:10

I have a windows form application that uses a Shared class to house all of the common objects for the application. The settings class has a collection of objects that do thi

4条回答
  •  礼貌的吻别
    2020-12-22 07:58

    You should look at the documentation for the Invoke method on the Form. It will allow you to make the code that updates the form run on the thread that owns the form, (which it must do, Windows forms are not thread safe). Something like Private Delegate Sub UpdateStatusDelegate(ByVal newStatus as String)

    Public sub UpdateStatus(ByVal newStatus as String) If Me.InvokeRequired Then Dim d As New UpdateStatusDelegate(AddressOf UpdateStatus) Me.Invoke(d,new Object() {newStatus}) Else 'Update the form status End If

    If you provide some sample code I would be happy to provide a more tailored example.

    Edit after OP said they are using InvokeRequired.

    Before calling InvokeRequired, check that the form handle has been created, there is a HandleCreated property I belive. InvokeRequired always returns false if the control doesn't currently have a handle, this would then mean the code is not thread safe even though you have done the right thing to make it so. Update your question when you find out. Some sample code would be helpful too.

提交回复
热议问题