How do I start and stop threads from the GUI?

  • Thread starter Thread starter Russ Ryba
  • Start date Start date
R

Russ Ryba

I'm working on a threaded socket server for PPC. The problem I'm having
is I can't seem to start and stop the socket from the GUI. I'm sure
there is something required with Invoke, but I am not sure where to start.

I want to listen when a toolbar button is pushed, and stop listening
when the toolbar button is pushed again. I think the listener may have
to poll the toolbar button state. I'm unclear if the listening thread
can READ control properties directly, or if they need to synclock or
invoke things too.

Right now it works like this. (OK, it doesn't work at all like this.)

-----------------------------------------------------------------------
Private Sub ToolBar1_ButtonClick(...) Handles ToolBar1.ButtonClick
'Evaluate the Button property to determine which
' button was clicked.
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0 'Connect
If e.Button.Pushed Then
Debug.WriteLine("Start Listening")
StartListening()
Else
Debug.WriteLine("Stop Listening")
StopListening()
End If
End Select
End Sub

Private Sub StartListening()
If ListenerThread Is Nothing Then
ListenerThread = New Thread(AddressOf IHearYa)
End If
Try
ListenerThread.Start()
Catch ex As Exception
Debug.WriteLine("Error in StartListening, " & ex.Message _
& vbCrLf & ex.ToString)
End Try
End Sub

Private Sub StopListening()
Try
'Kill the thread by setting it to nothing
' Will this work?
ListenerThread = Nothing
Catch ex As Exception
Debug.WriteLine("Exception in stopListening, " & _
ex.Message & vbCrLf & ex.ToString)
End Try
End Sub

Private Sub IHearYA()
'Starts a Socket
'Listen
'Bind
'Repeat until Toolbar button.pushed is false
End Sub
 
Do not kill threads. Stop threads gracefully by setting a flag that is
checked in the thread proc.
 
Depends on how you want it to work, but if you use the ThreadEx class from
OpenNETCF you can pause and resume a thread. That's probably the best bet.

-Chris
 
Back
Top