473,788 Members | 2,548 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(B yVal filename As String)
Event EndFileProc(ByV al filename As String)
Event ProcProgress(By Val filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBa se
Public ReadOnly WorkThread As System.Threadin g.Thread

Public Sub New()
Me.WorkThread = New System.Threadin g.Thread(Addres sOf Me.StartTask)
End Sub

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

Public Overridable Sub [Stop]()
Me.WorkThread.A bort()
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 ThreadWrapperBa se
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(_Fi leToProcess)
End Sub

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

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[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 2548
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.CurrentT hread.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_sbcg lobal[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(B yVal filename As String)
Event EndFileProc(ByV al filename As String)
Event ProcProgress(By Val filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBa se
Public ReadOnly WorkThread As System.Threadin g.Thread

Public Sub New()
Me.WorkThread = New System.Threadin g.Thread(Addres sOf Me.StartTask) End Sub

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

Public Overridable Sub [Stop]()
Me.WorkThread.A bort()
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 ThreadWrapperBa se
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(_Fi leToProcess)
End Sub

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

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[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.CurrentT hread.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_sbcg lobal[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(B yVal filename As String)
Event EndFileProc(ByV al filename As String)
Event ProcProgress(By Val filename As String, ByVal percent As Integer)
End Interface

Public MustInherit Class ThreadWrapperBa se
Public ReadOnly WorkThread As System.Threadin g.Thread

Public Sub New()
Me.WorkThread = New System.Threadin g.Thread(Addres sOf

Me.StartTask)
End Sub

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

Public Overridable Sub [Stop]()
Me.WorkThread.A bort()
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 ThreadWrapperBa se
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(_Fi leToProcess)
End Sub

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

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[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_lunch meat_[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_sbcg lobal[dot]]net"> wrote in
message news:lg******** *************** *****@40tude.ne t...
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.CurrentT hread.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_sbcg lobal[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(B yVal filename As String)
Event EndFileProc(ByV al filename As String)
Event ProcProgress(By Val filename As String, ByVal percent As
Integer)
End Interface

Public MustInherit Class ThreadWrapperBa se
Public ReadOnly WorkThread As System.Threadin g.Thread

Public Sub New()
Me.WorkThread = New System.Threadin g.Thread(Addres sOf

Me.StartTask)
End Sub

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

Public Overridable Sub [Stop]()
Me.WorkThread.A bort()
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 ThreadWrapperBa se
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(_Fi leToProcess)
End Sub

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

--
Chris

dunawayc[AT]sbcglobal_lunch meat_[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_lunch meat_[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
2108
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 form and I can process what I want based on them. If I try to catch raised events by this class but within a form located in a different assembly, I am not able to
77
5390
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
6
2885
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 TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
0
987
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 threading no Events are received. Code:
13
1950
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 indicates that the forms timer does not operate on its own thread and will not cause a query to be sent to use the single SqlConnection while another is being executed from the handling of other events. Is that right? I just want to be 100% sure...
4
1598
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 the way my component raises events to follow the Framework Design Guides verbatim; ie (object sender, EventArgs (or some subclass there of) e). The thing that was not explained is why should I need to cast the sender to 'object' when I know...
2
1697
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 the parent form. This is fine. The problem occurs when I want to raise an event for a user clicking on one of the clsRecords which are on the grid. So I've placed: Public Event GridRecordClicked(ByVal rec As clsGridRecord,
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 many files may appear (not necessarily at once, but close enough) and will fire the file system watcher events, opening up many threads. Is there a way I can limit the threads that open, yet still keep the new file information in some sort of queue...
1
1455
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 of the method, the class is suppose to fire events such as ProgressChanged. And it raises Completed and Failed event on completion and error
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10374
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10121
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5404
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2898
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.