473,783 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
BackgroundWorke r 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 1096
On Mon, 04 Aug 2008 11:12:37 -0700, Rymfax <cw*****@bigban gllc.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
BackgroundWorke r 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 Synchronization Context for the thread used
to initialize the BackgroundWorke r 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 ISynchronizeInv oke 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
4071
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
22891
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 when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
2
1219
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 program never returned from the event subscription. No errors were thrown but the program just hung there. It was resolved by another member of the team by moving the location of a System.Threading.Monitor class. This solved the problem but I would...
30
3659
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 it seems to me that Microsoft has developed similar functionality via two keywords. I do understand that an event offers better encapsulation as the underlying delegate is private, but is that all ? -- Regards
9
1834
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using CSharpLib;
13
1972
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
1949
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 Windows Callback is to generalise our API so that it can be compatible with any other language e.g. C++. Using normal delegates will not help us since delegates is a VB.Net feature and any other language cant make a use of it. Could please
5
2401
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 many heavy duty pro programmers in this newsgroup have actually used a delegate in their code, say in the last year of coding. By "use" I mean constructed a delegate and using it in an Event Source class and an Event Receiver class. Interfaces...
7
3422
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 uses the "WithEvents" and events. There is another example on page 124 that shows how to use delegates to sort an array. I don't understand the difference between events and delegates. Are they redundant features? How do I decide which to use? ...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.