473,503 Members | 10,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading and raising events

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.
Nov 21 '05 #1
3 2529
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1c*******************************@40tude.net. ..
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.

Nov 21 '05 #2
On Mon, 4 Oct 2004 13:35:38 -0500, CJ Taylor wrote:
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1c*******************************@40tude.net. ..
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.
Nov 21 '05 #3
Not a problem... happy to help.
"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:lg****************************@40tude.net...
On Mon, 4 Oct 2004 13:35:38 -0500, CJ Taylor wrote:
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" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1c*******************************@40tude.net. ..
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.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2069
by: serge calderara | last post by:
Dear all, I have a class wich is raising events as normally it should do. having a form in the same assembly wich is catching those events works fne. Raise events gets catch normaly within the...
77
5208
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
2836
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
0
976
by: BenLeino | last post by:
Hi out there, I have a little problem with threading an event receiving. I have a custom Class (DLL) that raises Events. When I run the Instance without threading it works fine. When I do...
13
1925
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
4
1580
by: Dave A | last post by:
I am developing a somewhat complex component at the moment and coincidently I am also reading the Framework Design Guidelines book. After reading the section about event raising I have re-written...
2
1679
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
1
245
by: Jason | last post by:
I'm building a windows service which will monitor the file system for new files, process the information in any new file that appears, and put that information into a database. My worry is that...
1
1445
by: Ajak | last post by:
Hi all, I would like to write a class (Task) with a method to do some lengthy process based on several of the class properties. The method is running on different thread. During the execution...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5602
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.