Threading and raising events

C

Chris Dunaway

Consider the following simple classes/interfaces defined below. When the
derived class raises the events, on which thread is the event code run? Do
I need to do anything to catch the events in my main app? What threading
issues do I have if several of these fileproc classes are instantiated and
run at the same time?

'*** BEGIN CODE
Public Interface IFileProc
ReadOnly Property FileMask() As String
Function Process(ByVal filename As String) As Integer
Event BeginFileProc(ByVal filename As String)
Event EndFileProc(ByVal filename As String)
Event ProcProgress(ByVal filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBase
Public ReadOnly WorkThread As System.Threading.Thread

Public Sub New()
Me.WorkThread = New System.Threading.Thread(AddressOf Me.StartTask)
End Sub

Public Overridable Sub Start()
Me.WorkThread.Start()
End Sub

Public Overridable Sub [Stop]()
Me.WorkThread.Abort()
End Sub

Private _IsCompleted As Boolean
Public ReadOnly Property IsCompleted() As Boolean
Get
Return _IsCompleted
End Get
End Property

Private Sub StartTask()
_IsCompleted = False
DoTask()
_IsCompleted = True
End Sub

Protected MustOverride Sub DoTask()
End Class

Public Class FileProcClass()
Inherits ThreadWrapperBase
Implement IFileProc

Private _FileToProcess As String

'This is the sub where the file processing actually takes place
Protected Overrides Sub DoTask()
RaiseEvent BeginFileProc(_FileToProcess)
'Process file here
RaiseEvent EndFileProc(_FileToProcess)
End Sub

Public Function Process(...) As Integer Implements IFileProc.Process
_FileToProcess = filename
Start()
End Function
End Class
'*** END CODE

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

CJ Taylor

The event is raised on the thread that called it. And executed on the
thread that called it (regardless of where the sink was created).

The easiest way I've seen to do this is to create an instance of the Control
class on the thread you want to execute on.

I.e. you want a thread to execute on your main thread from another, before
creating the other thread, create an instance of the Control class (watch
for getting a Handle, sometimes that can cause a problem.) on that main
thread.

Then, when you receive an event, use the Controls Invoke method to invoke
whatever method it is you want on the main thread (the *true* event handler
if you will). This invokes the method on the thread it was created on,
doing all the dirty work of crossing over to the main thread from the
secondary thread. And there you go.

Verify by using the Thread.CurrentThread.HashCode.. Or name your threads...

Might have to watch for deadlock though, as your second thread will
effectivly be waiting on your main thread to complete the task before it
continues on.

HTH,
CJ


Chris Dunaway said:
Consider the following simple classes/interfaces defined below. When the
derived class raises the events, on which thread is the event code run? Do
I need to do anything to catch the events in my main app? What threading
issues do I have if several of these fileproc classes are instantiated and
run at the same time?

'*** BEGIN CODE
Public Interface IFileProc
ReadOnly Property FileMask() As String
Function Process(ByVal filename As String) As Integer
Event BeginFileProc(ByVal filename As String)
Event EndFileProc(ByVal filename As String)
Event ProcProgress(ByVal filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBase
Public ReadOnly WorkThread As System.Threading.Thread

Public Sub New()
Me.WorkThread = New System.Threading.Thread(AddressOf Me.StartTask)
End Sub

Public Overridable Sub Start()
Me.WorkThread.Start()
End Sub

Public Overridable Sub [Stop]()
Me.WorkThread.Abort()
End Sub

Private _IsCompleted As Boolean
Public ReadOnly Property IsCompleted() As Boolean
Get
Return _IsCompleted
End Get
End Property

Private Sub StartTask()
_IsCompleted = False
DoTask()
_IsCompleted = True
End Sub

Protected MustOverride Sub DoTask()
End Class

Public Class FileProcClass()
Inherits ThreadWrapperBase
Implement IFileProc

Private _FileToProcess As String

'This is the sub where the file processing actually takes place
Protected Overrides Sub DoTask()
RaiseEvent BeginFileProc(_FileToProcess)
'Process file here
RaiseEvent EndFileProc(_FileToProcess)
End Sub

Public Function Process(...) As Integer Implements IFileProc.Process
_FileToProcess = filename
Start()
End Function
End Class
'*** END CODE

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

Chris Dunaway

The event is raised on the thread that called it. And executed on the
thread that called it (regardless of where the sink was created).

The easiest way I've seen to do this is to create an instance of the Control
class on the thread you want to execute on.

I.e. you want a thread to execute on your main thread from another, before
creating the other thread, create an instance of the Control class (watch
for getting a Handle, sometimes that can cause a problem.) on that main
thread.

Then, when you receive an event, use the Controls Invoke method to invoke
whatever method it is you want on the main thread (the *true* event handler
if you will). This invokes the method on the thread it was created on,
doing all the dirty work of crossing over to the main thread from the
secondary thread. And there you go.

Verify by using the Thread.CurrentThread.HashCode.. Or name your threads...

Might have to watch for deadlock though, as your second thread will
effectivly be waiting on your main thread to complete the task before it
continues on.

HTH,
CJ


Chris Dunaway said:
Consider the following simple classes/interfaces defined below. When the
derived class raises the events, on which thread is the event code run? Do
I need to do anything to catch the events in my main app? What threading
issues do I have if several of these fileproc classes are instantiated and
run at the same time?

'*** BEGIN CODE
Public Interface IFileProc
ReadOnly Property FileMask() As String
Function Process(ByVal filename As String) As Integer
Event BeginFileProc(ByVal filename As String)
Event EndFileProc(ByVal filename As String)
Event ProcProgress(ByVal filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBase
Public ReadOnly WorkThread As System.Threading.Thread

Public Sub New()
Me.WorkThread = New System.Threading.Thread(AddressOf Me.StartTask)
End Sub

Public Overridable Sub Start()
Me.WorkThread.Start()
End Sub

Public Overridable Sub [Stop]()
Me.WorkThread.Abort()
End Sub

Private _IsCompleted As Boolean
Public ReadOnly Property IsCompleted() As Boolean
Get
Return _IsCompleted
End Get
End Property

Private Sub StartTask()
_IsCompleted = False
DoTask()
_IsCompleted = True
End Sub

Protected MustOverride Sub DoTask()
End Class

Public Class FileProcClass()
Inherits ThreadWrapperBase
Implement IFileProc

Private _FileToProcess As String

'This is the sub where the file processing actually takes place
Protected Overrides Sub DoTask()
RaiseEvent BeginFileProc(_FileToProcess)
'Process file here
RaiseEvent EndFileProc(_FileToProcess)
End Sub

Public Function Process(...) As Integer Implements IFileProc.Process
_FileToProcess = filename
Start()
End Function
End Class
'*** END CODE

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Thanks for the response. I think you have helped me.
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
C

CJ Taylor

Not a problem... happy to help.


Chris Dunaway said:
The event is raised on the thread that called it. And executed on the
thread that called it (regardless of where the sink was created).

The easiest way I've seen to do this is to create an instance of the
Control
class on the thread you want to execute on.

I.e. you want a thread to execute on your main thread from another,
before
creating the other thread, create an instance of the Control class (watch
for getting a Handle, sometimes that can cause a problem.) on that main
thread.

Then, when you receive an event, use the Controls Invoke method to invoke
whatever method it is you want on the main thread (the *true* event
handler
if you will). This invokes the method on the thread it was created on,
doing all the dirty work of crossing over to the main thread from the
secondary thread. And there you go.

Verify by using the Thread.CurrentThread.HashCode.. Or name your
threads...

Might have to watch for deadlock though, as your second thread will
effectivly be waiting on your main thread to complete the task before it
continues on.

HTH,
CJ


Chris Dunaway said:
Consider the following simple classes/interfaces defined below. When
the
derived class raises the events, on which thread is the event code run? Do
I need to do anything to catch the events in my main app? What
threading
issues do I have if several of these fileproc classes are instantiated
and
run at the same time?

'*** BEGIN CODE
Public Interface IFileProc
ReadOnly Property FileMask() As String
Function Process(ByVal filename As String) As Integer
Event BeginFileProc(ByVal filename As String)
Event EndFileProc(ByVal filename As String)
Event ProcProgress(ByVal filename As String, ByVal percent As
Integer)
End Interface

Public MustInherit Class ThreadWrapperBase
Public ReadOnly WorkThread As System.Threading.Thread

Public Sub New()
Me.WorkThread = New System.Threading.Thread(AddressOf Me.StartTask)
End Sub

Public Overridable Sub Start()
Me.WorkThread.Start()
End Sub

Public Overridable Sub [Stop]()
Me.WorkThread.Abort()
End Sub

Private _IsCompleted As Boolean
Public ReadOnly Property IsCompleted() As Boolean
Get
Return _IsCompleted
End Get
End Property

Private Sub StartTask()
_IsCompleted = False
DoTask()
_IsCompleted = True
End Sub

Protected MustOverride Sub DoTask()
End Class

Public Class FileProcClass()
Inherits ThreadWrapperBase
Implement IFileProc

Private _FileToProcess As String

'This is the sub where the file processing actually takes place
Protected Overrides Sub DoTask()
RaiseEvent BeginFileProc(_FileToProcess)
'Process file here
RaiseEvent EndFileProc(_FileToProcess)
End Sub

Public Function Process(...) As Integer Implements IFileProc.Process
_FileToProcess = filename
Start()
End Function
End Class
'*** END CODE

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.

Thanks for the response. I think you have helped me.
--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top