473,388 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

BackgroundWorker who knows how the magic works?

Just wondering if anyone knows how this component works.
If you set it up in the context of an Application.Run it works on it's
worker thread but it updates the progress on the UI thread.
My question is, how does it know which thread to use?

I used Reflector to track down the fact that it gets the delegates to call
via base.Events[SomeEventKey] which derives from Component but how does it
know what to put into base.Events?

I'm very happy the component is there as it's very useful for .NET 2.0
considering the changes to unhandled exceptions on threads. However i'd like
to know if it is possible to break it and how I could break it before I
start scattering instances of it throughout my projects.

Kind Regards

Simon Tamman
Nov 6 '06 #1
2 3011
Hi Simon,
Just wondering if anyone knows how this component works.
If you set it up in the context of an Application.Run it works on it's
worker thread but it updates the progress on the UI thread.
My question is, how does it know which thread to use?
Basically, BackgroundWorker stores a reference to the current
SynchronizationContext when it begins the asynchronous process and uses the
reference after the process has completed to marshal the callback onto the UI
thread. (Check out the Send method)

"SynchronizationContext"
http://msdn2.microsoft.com/en-us/lib...xt(VS.80).aspx

Synchronization works with ASP.NET and WinForms through derived
SynchronizationContext implementations. The ASP.NET implementation is
internal, IIRC. The WinForms implementation is public:

"WindowsFormsSynchronizationContext"
http://msdn2.microsoft.com/en-us/lib...xt(VS.80).aspx

(You really don't need to be aware of the implementation to use the current
SynchronizationContext: SynchronizationContext.Current)
I used Reflector to track down the fact that it gets the delegates to call
via base.Events[SomeEventKey] which derives from Component but how does it
know what to put into base.Events?
EventHandlerList stores keyed delegates that are registered to handle specific
events. The key is proprietary. Usually, static System.Object instances are
used to key events in the list.

The purpose of an event handler list is to save memory when a component
exposes many events but when only a handful are used simultaneously. This
way, it is no longer necessary to make room for the memory required to store
the private delegate fields for every event.
I'm very happy the component is there as it's very useful for .NET 2.0
considering the changes to unhandled exceptions on threads. However i'd like
to know if it is possible to break it and how I could break it before I
start scattering instances of it throughout my projects.
Break what exactly?

--
Dave Sexton


Nov 6 '06 #2
Thank you very much for the information Dave.

SynchronizationContext.Current

is new to me and is valuable information.

In terms of breaking it:

If I marshalled a worker progress to a different UI thread, that'd kill it.
Not likely that i'd ever be in a position to do that but it's nice to know
the boundaries of anything automagic.

Thanks again!

Simon.

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:e4*************@TK2MSFTNGP02.phx.gbl...
Hi Simon,
Just wondering if anyone knows how this component works.
If you set it up in the context of an Application.Run it works on it's
worker thread but it updates the progress on the UI thread.
My question is, how does it know which thread to use?

Basically, BackgroundWorker stores a reference to the current
SynchronizationContext when it begins the asynchronous process and uses
the
reference after the process has completed to marshal the callback onto the
UI
thread. (Check out the Send method)

"SynchronizationContext"
http://msdn2.microsoft.com/en-us/lib...xt(VS.80).aspx
>
Synchronization works with ASP.NET and WinForms through derived
SynchronizationContext implementations. The ASP.NET implementation is
internal, IIRC. The WinForms implementation is public:

"WindowsFormsSynchronizationContext"
http://msdn2.microsoft.com/en-us/lib...xt(VS.80).aspx
>
(You really don't need to be aware of the implementation to use the
current
SynchronizationContext: SynchronizationContext.Current)
I used Reflector to track down the fact that it gets the delegates to
call
via base.Events[SomeEventKey] which derives from Component but how does
it
know what to put into base.Events?

EventHandlerList stores keyed delegates that are registered to handle
specific
events. The key is proprietary. Usually, static System.Object instances
are
used to key events in the list.

The purpose of an event handler list is to save memory when a component
exposes many events but when only a handful are used simultaneously. This
way, it is no longer necessary to make room for the memory required to
store
the private delegate fields for every event.
I'm very happy the component is there as it's very useful for .NET 2.0
considering the changes to unhandled exceptions on threads. However i'd
like
to know if it is possible to break it and how I could break it before I
start scattering instances of it throughout my projects.

Break what exactly?

--
Dave Sexton


Nov 8 '06 #3

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

Similar topics

3
by: chris | last post by:
hello, I can't seem to make this work: VS2005 I have a simple program that uses a backgroundworker control to execute a long process (webservice call) if that webservice call fails, i want to...
2
by: dm1608 | last post by:
Hi -- I have a C# application that basically has a button that executes a SQL Reader to loop thru a rather large resul set. Thru each interation of the reader object, I check to see if a file...
2
by: Sebastian Crewe | last post by:
Greetings, I was much encouraged to see the new BackgroundWorker class in .NET v2. On the face of it, much easier to use than the various delegates and events of yore, though I imagine the same...
4
by: Larry R | last post by:
I am trying to set a panel (that holds a progress image) to be visible when a long running process is happening. Sounds simple, right :) What happens is the panel never becomes visible. The load...
5
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
9
by: =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post by:
Hello, In my ASP.Net app I'm launching a BackgroundWorker thread in my Page_Load function. In that thread I'm attempting to connect to a SQL server using this connection string "Initial...
14
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
2
by: Chris | last post by:
When I try to access the backgroundWorker.CancellationPending property I get the following exception: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in...
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.