Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

Can a Backgroundworker have or reference an eventhandler?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rof - 15 Jan 2008 12:45 GMT
I have an app which has to wait for messages (using port.DataReceived)
and process them when they come in. As I expect the traffic to be
heavy, I want to do this using a Backgroundworker. Can I add an
eventhandler in "DoIt" and expect the delegate to which it points to
be called in the background?

The other obvious way to do it is to continually check
port.BytesToRead, but I'm not sure that is foolproof - VS help is less
than helpful, here.

TIA,
Peter Royle
Tom Dacon - 16 Jan 2008 16:32 GMT
Yes. Events raised by the process that's being run by the background worker
will fire properly. However, the event handler will not be running on the UI
thread, so you can't do anything in that event handler to affect the
controls of a Windows Forms front end.

A good architecture for something like this is for your worker process to
not even know that it's being run on a separate thread in a
BackgroundWorker. The simple example in MSDN help doesn't separate things
this way, but that's probably reasonable for their purposes. The way you do
this is to have the worker started from a BackgroundWorker object via
RunAsync, then for the worker to communicate with its owning object only
through events. In the event handlers, the owning object then sends messages
to the BackgroundWorker object through its ReportProgress method, which in
turn raises BackgroundWorker events ON THE UI THREAD. The UserState event
argument is how you communicate data from the worker all the way up the
chain to the UI thread.

HTH,
Tom Dacon
Dacon Software Consulting

>I have an app which has to wait for messages (using port.DataReceived)
> and process them when they come in. As I expect the traffic to be
[quoted text clipped - 8 lines]
> TIA,
> Peter Royle

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.