Thread Question

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Hi,
I use this to set focus while running my code on a thread:
Visual Basic:
    Delegate Sub SetFocusCallback()
    Private Sub SetFocus()
        On Error Resume Next
        If Me.ProcessButtonX.InvokeRequired Then
            Dim d As New SetFocusCallback(AddressOf SetFocus)
            Me.Invoke(d)
        Else
            Me.ProcessButtonX.Focus()
        End If
    End Sub
I can do everything I need but I cannot use Me.Close in a thread!
How should I change the above code to close the form inside a thread for me and then finish thread?
 
When running from a background thread Me would refer to the thread's class rather than the form - you would either need to pass a reference to the form to the thread or invoke the close method similar to how you are invoking a method in your code above.
 
sorry but i could not understand you?
so how can i unload the form in this thread?

"If Me.Form_Closing Then" how it should be?
 
If you need to call the form's close method from a thread then you will need to use Invoke - something similar to
Visual Basic:
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim t As New Thread(testThread)
    t.Start()
End Sub
Delegate Sub test()
Private Sub testThread()

    If InvokeRequired Then
        Dim d As New test(Me.Close)
        Invoke(d)
    Else
        Me.Close()
    End If
End Sub
should do it.

If you want to handle the closing event in a non-ui thread then that is a different matter - the event will be raised on the UI thread and you would need someway to communicate with your thread.
 
Back
Top