473,386 Members | 1,745 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,386 software developers and data experts.

Help with Events, Delegates and Threading

I was hoping someone could help me figure something.

I have a class that is a downloader and I've created some events on it
so that a Windows Form can use those to be notified when to update
screen information. The problem I'm running into is that when the
downloader runs on a different thread and those Events fire, if the
corresponding method on the GUI for that even updates any Controls, I
get a threading error. I know that I can solve this by using
delegates on my Windows Form, but I was wondering if there was any way
to code the downloader object so that the delegates were taking care
of on that side. I only ask because I think if you use the
BackgroundWorker and call the ReportProgress() method that the
corresponding method for the event on the Windows Form can update
Controls just fine. How is it able to do that?

Any help would be appreciated.

Thanks!
Aug 4 '08 #1
1 1081
On Mon, 04 Aug 2008 11:12:37 -0700, Rymfax <cw*****@bigbangllc.comwrote:
[...] I was wondering if there was any way
to code the downloader object so that the delegates were taking care
of on that side. I only ask because I think if you use the
BackgroundWorker and call the ReportProgress() method that the
corresponding method for the event on the Windows Form can update
Controls just fine. How is it able to do that?
I believe that it captures the SynchronizationContext for the thread used
to initialize the BackgroundWorker instance, and then uses the context to
invoke delegates on the correct thread. You could to that.
Alternatively, you can traverse the invocation list for the event
explicitly, checking the Target for each Delegate instance to see if it
implements ISynchronizeInvoke and use that to call Invoke() (but this will
only work if the delegate references a member of a GUI element inheriting,
for example, Control).

Personally, it's my opinion that GUI code should know about GUI stuff and
worker code should not. This means that it's the event _handler_ that
should deal with the threading issue as necessary, rather than your
downloader class. Note that with anonymous methods, creating the delegate
to be invoked on the GUI thread is very concise and elegant (in my
opinion).

Pete
Aug 4 '08 #2

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

Similar topics

22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
by: John Olbert | last post by:
Could someone recommend an indepth book(s) or set of articles on Events, Threading and Delegates under C#? We have run into a problem in which when an event was subscribed in code that the...
30
by: Burkhard | last post by:
Hi, I am new to C# (with long year experience in C++) and I am a bit confused by the language construct of events. What is it I can do with events that I cannot do with delegates? At the moment...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.