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. 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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |