Listening for threading events

  • Thread starter Thread starter Keith Patrick
  • Start date Start date
K

Keith Patrick

I know this should be a simple thing, but I can't seem to have any luck
finding documentation on it: How does one listen for when a thread
starts/stops/suspends? System.Threading.Thread has no OnStart, etc. events,
and it is sealed so I can't make my own. What I'm trying to do is have my
app spawn an execution thread, and perform certain actions when it does
certain things.
 
Hi Kieth

One option is you can try polling a thread using Thread.IsAlive, or if u
want to wait till it ends use Thread.Join.


--

Regards
Mandar

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I know this should be a simple thing, but I can't seem to have any luck
finding documentation on it: How does one listen for when a thread
starts/stops/suspends? System.Threading.Thread has no OnStart, etc. events,
and it is sealed so I can't make my own. What I'm trying to do is have my
app spawn an execution thread, and perform certain actions when it does
certain things.

What I do is create a thread that fires events. When the thread
starts, it fires its own "OnStart" event, etc.

Here's a bit of a skeleton; I've cut out a bunch of my own code,
but you should be able to design your own class with this easily.

// CHRIS





Public Class WorkerThread

'Events
Public Event Stopped()
Public Event Started()
Public Event StatusUpdate(ByVal szStatus As String)

Private m_bRunning As Boolean = False 'Running?
Private m_bStop As Boolean = False 'Stopping?
Private m_bEnabled As Boolean = True 'Enabled?
Private m_Thread As Thread 'Thread For Socket
Listening

Private m_LastMailCheck As DateTime = New DateTime
Private m_szLastError As String

Private m_ProgramSettings As ProgramSettings
Private m_DataSet_POP3 As DataSet = Nothing

Public Sub Start(ByRef theProgramSettings As ProgramSettings, _
ByVal bInitiallyEnabled As Boolean)

Dim threadStart As ThreadStart

threadStart = New ThreadStart(AddressOf Me.Run)
m_Thread = New Thread(threadStart)
m_Thread.Name = "Mail Check Thread"
m_bStop = False
m_bRunning = True
m_ProgramSettings = theProgramSettings
m_bEnabled = bInitiallyEnabled
m_Thread.Start()

End Sub

Public Sub Run()

RaiseEvent Started()

Do While m_bStop = False

'Check For Work
If HaveWorkToDo() = True Then

if bEnabled = True Then
DoTheWork()
RaiseEvent StatusUpdate("Status Info")
End If

End If

'Sleep A Little (Else we'll max out the CPU)
System.Threading.Thread.Sleep(250)

Loop

'Update Running Status And Raise Stopped() event
m_bRunning = False
m_bStop = False
RaiseEvent Stopped()
Debug.WriteLine("STOPPED")
Return

End Sub

End Class
 
(SNIP)
What I do is create a thread that fires events. When the thread
starts, it fires its own "OnStart" event, etc.
// CHRIS

Er- Actually, what I meant to say was that I create a CLASS that fires
events, and in the class is a "Start" function that starts the
background thread. Then the thread function (the "Run()" function in
my example) fires events as it starts, stops, etc..

// CHRIS
 
Back
Top