By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

Threading & raising event question

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.