How to do multi-threading in Vc++ .net

  • Thread starter Thread starter Kueishiong Tu
  • Start date Start date
K

Kueishiong Tu

I have a window form application. How do I do multi-
threading so that my application won't be tied up while
trying to receive the data from the internet? Coding
samples will be helpful.
 
Chris Sells wrote a very nice "Safe, Simple Multithreading in Windows Forms"
series of articles just on this topic. They should be available in the MSDN
Library as well as on the www.gotdotnet.com website. Sample code is given in
C# but the same ideas apply to Managed C++.
 
I have a window form application. It requires to retrieve
data from a web site. Since the web request is very time
consuming, so I create a work thread to do the job.
How does the window main form (which is the main thread)
know when the work thread has finished the job so that
it can display the data received on the form? I do not
want to use a global variable since if the main form is
continuously checking for the global variable, it will
not be able to process the window UI events. I try to
raise an event when the work thread's job is done,
but how can the main thread detect this event?
..
Thank you for your information on Chris Sells articles,
but I do not find the answer from these articles.
 
Put the code that performs the work of the worker thread in a class member
subroutine or function. Declare the class in your form with the keyword
WithEvents. Raise an event in the class and then write an event handler in
your form that catches this class's event.

Public Class1
Public Event WorkDone()

Public Sub DoWork

' ....do work here....

RaiseEvent WorkDone
End Sub

End Class

In your form, declare the class WithEvents and catch the event by writing an
event handler.

Private WithEvents MyClass as Class1
Private Sub HandleTheEvent() Handles MyClass.WorkDone
'insert whatever code to handle the event
End Sub
 
Christine said:
Put the code that performs the work of the worker thread in a class
member subroutine or function. Declare the class in your form with
the keyword WithEvents. Raise an event in the class and then write
an event handler in your form that catches this class's event.

Public Class1
Public Event WorkDone()

Public Sub DoWork

' ....do work here....

RaiseEvent WorkDone
End Sub

End Class

In your form, declare the class WithEvents and catch the event by
writing an event handler.

Private WithEvents MyClass as Class1
Private Sub HandleTheEvent() Handles MyClass.WorkDone
'insert whatever code to handle the event
End Sub

Christine,

The problem with this code is that when the worker thread raises the event,
it is the *worker thread* that ends up calling TheEvent() and not the GUI
thread. Its a cardinal sin in Win32 or Windows Forms to perform GUI work on
a thread other than the UI thread. Instead, the worker thread should call
Control.Invoke() on the form and pass a delegate. This way the delegate is
run by the UI thread and not the worker thread.

Richard
 
Back
Top