I've searched around and I want to abort a thread and restart it, something who should be really simple but no one is answering.
Basicly I have an user who conenct throught a form, when the user is authenthified it raise an event to connect the user bringing a new form on another thread, so if the user disconnect I end the thread and bring him to the connection form but if he try to connect again how do I start the thread again
Starting connected form
Private Sub sAuthentified(ByVal Sender As Coms, ByVal sTemp As String) Handles mComs.sAuthentified
If (Equals(Sender.AES_Decrypt(sTemp), "$%?SuccesS&*(")) Then
Dim d1 As New HideForm(AddressOf Hide)
Me.Invoke(d1)
t1.Start()
Else
ToolTip1.Show(String.Empty, UsernameField, 103, 10, 1)
ToolTip1.Show("Matricule et/ou password ne sont pas valide.", UsernameField, 103, 10, 1000)
End If
End Sub
Ending the connected form
Private Sub Me_Disconnect(ByVal Sender As Coms) Handles mComs.Disconnect
mComs = Nothing
t1.Abort()
connectedForm.Dispose()
Dim d As New ShowForm(AddressOf Show)
Me.Invoke(d)
End Sub
Started by t1
Private Sub newForm()
connectedForm = New Connected(mComs, sUser_sPass)
connectedForm.ShowDialog()
connectedForm.Dispose()
mComs.sendMessage(Coms.enumTags.Disconnect)
End Sub
I've searched around and I want to abort a thread and restart it, something who should be really simple but no one is answering.
You can't - it's as simple as that. Once a thread has been successfully aborted (i.e. it really has completed, and is in a state of Aborted
, not just AbortRequested
) you can't restart it.
It sounds like you should just be creating a new thread - if indeed it's appropriate to use multiple threads at all in this case. (It's not clear why you'd want to have multiple UI threads. Normally there's a single UI thread, but possibly multiple non-UI threads. There are exceptions to this rule, but you should have a really good reason...)
I'd also avoid aborting threads - the only thread you can really safely abort (and then continue with the app) is the current thread, and even that's normally just a shortcut to avoid better design. Otherwise you don't know what the thread you're aborting is really doing.
See more on this question at Stackoverflow