ADO DOT NET Posted December 24, 2007 Posted December 24, 2007 Hi, I use this to set focus while running my code on a thread: 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? Quote
Administrators PlausiblyDamp Posted December 26, 2007 Administrators Posted December 26, 2007 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ADO DOT NET Posted December 27, 2007 Author Posted December 27, 2007 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? Quote
Administrators PlausiblyDamp Posted December 27, 2007 Administrators Posted December 27, 2007 If you need to call the form's close method from a thread then you will need to use Invoke - something similar to 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.