473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threading & raising event question

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
respectively.

The question is, how do I raise the event in the main thread (where the
class is instantiated)
without having to write the InvokeRequired checking for each controls I want
to update, say,
ProgressBar and Labels. In other words, how to fire the events that this
class exposes so
that at the receiving end of the event, I do not have to write the tedious
Invoke function to
access/alter its controls properties.

FYI, the class is not a control. Just a plain Class Library.

Below is the simplified class structure I did:-

Class Task
Private m_prop1 as integer
Private m_prop2 as integer

Public Event ProgressChanged(ByVal currentvalue as integer)
Public Event Completed()
Public Event Failed(ByVal errmessage as String)

Public Property Property1() As Integer
....
End Property

Public Property Property2() As Integer
....
End Property

Public Sub ProcessTask()
Dim t As New Thead(AdressOf DoWork)
t.Start()
End

Private Sub DoWork()
Do
... (do processing here)
... (fires ProgressChanged event during the processing - RaiseEvent
ProgressChanged(val))
Loop While ....

RaiseEvent Completed()
End Sub

End Class

Thanks in advance.

Razak.
Jun 27 '07 #1
1 1440
"Ajak" <aj***********@gmail.comschrieb
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
respectively.

The question is, how do I raise the event in the main thread (where
the class is instantiated)
without having to write the InvokeRequired checking for each
controls I want to update, say,
ProgressBar and Labels.
You must use InvokeRequired unless you know that the procedure is running in
the none-UI thread. But you don't have to call it for any control, just once
at the start of the event handler. In any case you must call Invoke once.
In other words, how to fire the events that
this class exposes so
that at the receiving end of the event, I do not have to write the
tedious Invoke function to
access/alter its controls properties.

Without Invoke/Invokerequired is not possible.
FYI, the class is not a control. Just a plain Class Library.

Below is the simplified class structure I did:-

Class Task
Private m_prop1 as integer
Private m_prop2 as integer

Public Event ProgressChanged(ByVal currentvalue as integer)
Public Event Completed()
Public Event Failed(ByVal errmessage as String)

Public Property Property1() As Integer
....
End Property

Public Property Property2() As Integer
....
End Property

Public Sub ProcessTask()
Dim t As New Thead(AdressOf DoWork)
t.Start()
End

Private Sub DoWork()
Do
... (do processing here)
... (fires ProgressChanged event during the processing -
RaiseEvent ProgressChanged(val))
Loop While ....

RaiseEvent Completed()
End Sub

End Class

Thanks in advance.

Razak.

Armin

Jun 27 '07 #2

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

Similar topics

3
by: Trevor | last post by:
Hello, How can I setup an event in C#? I would like for class A to have an event, and class B to intercept the event. Class B should not be able to call the event itself, only class A. How is...
6
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
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...
4
by: Glenn Dekhayser | last post by:
Weird question, hopefully someone out there can help. I am basically iterating through a list of files, using QueueUserWorkItem to spawn a thread to process each file. I am using a wrapper...
3
by: Chris Dunaway | last post by:
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...
1
by: Brad Brening | last post by:
I am building a class that has a function that I would like to run asycronously. When the function is complete I would like to notify the calling thread by raising an event or using a delegate. The...
8
by: mmitchell | last post by:
I have a thread that does periodic database maintenance. While this process is going on I would like the rest of the application to hold off on accessing the database. I thought I could share a...
1
by: Paul Cheetham | last post by:
Hi, I have a thread in my application that is listening to the Serial port using GetCommEvents. When something is received, it generates an event, and then goes back to waiting for more data. -...
4
by: sloan | last post by:
I"m trying to figure out what concept I'm missing here, or if its not a good idea .. or what. Here is my example.. code is below. I have an employee class. It has an event that can be raised....
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.