Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Administrators
Posted
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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...