473,770 Members | 6,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add an event handler from other threads using BeginInvoke

I have a worker thread, and the thread needs to listen to WinForms Control's
SizeChanged event. Here's my code.

Control c = ...;
if (c.InvokeRequir ed) {
c.BeginInvoke(= ==don't know how to write here===);
} else {
c.SizeChanged += new EventHandler(th is.OnOwnerSizeC hanged);
}

I know I need to Invoke since my worker thread is not a UI thread. The
question is that, how could I write a C# code to create a delegate that adds
an event handler?

I tried:
c.BeginInvoke(n ew AddEventHandler Handler(c.add_S izeChanged), new
EventHandler(th is.OnOwnerSizeC hanged));

But this doesn't compile.
Nov 16 '05 #1
3 14106
Koji,

First, you have to define the delegate, this will take one parameter of
type EventHandler.

public delegate void AddEventHandler Callback(EventH andler handler);

Then, define the method.

public AddEventHandler (EventHandler handler)
{
// If null, then just get out.
if (handler == null)
// Get out.
return;

// Add the event.
c.SizeChanged += handler;

// That's all folks.
return;
}

Then, in your thread, you call invoke.
c.BeginInvoke(n ew AddEventHandler Callback(AddEve ntHandler), new
object[]{new EventHandler(th is.OnOwnerSizeC hanged)});

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Koji Ishii" <Ko*******@disc ussions.microso ft.com> wrote in message
news:DB******** *************** ***********@mic rosoft.com...
I have a worker thread, and the thread needs to listen to WinForms
Control's
SizeChanged event. Here's my code.

Control c = ...;
if (c.InvokeRequir ed) {
c.BeginInvoke(= ==don't know how to write here===);
} else {
c.SizeChanged += new EventHandler(th is.OnOwnerSizeC hanged);
}

I know I need to Invoke since my worker thread is not a UI thread. The
question is that, how could I write a C# code to create a delegate that
adds
an event handler?

I tried:
c.BeginInvoke(n ew AddEventHandler Handler(c.add_S izeChanged), new
EventHandler(th is.OnOwnerSizeC hanged));

But this doesn't compile.

Nov 16 '05 #2
So, what you are saying is that, basically, in C#, you cannot create a
delegate for get_, put_, add_, and remove_ accessors. Isn't this a problem?

I would like my code to work against any Control class, so I cannot add a
method to wrap these accessors.

It looks like you are saying I cannot do this in C#. I believe I can do this
in IL, so this is a C# limitation, correct?
Nov 16 '05 #3
Found solution, though it's not pretty.

private delegate IntPtr GetIntPtrDelega te();

Type type = target.GetType( );
Delegate funcptr = Delegate.Create Delegate(typeof (GetIntPtrDeleg ate),
target, "get_" + name);
target.Invoke(f uncptr, new object[0]);

Shouldn't this be part of the language?
Nov 16 '05 #4

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

Similar topics

8
6088
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
3
1955
by: James Dunkerley | last post by:
Hi, I am trying to create a class which downloads a web page in the background and then raises an event in the original thread in which it was created. This class is going to be the basis of a set of different applications and I want to hide the threading from the other layers. Any suggestions as to how to do this? Thanks James
1
3274
by: Mark Hoffman | last post by:
All, From what I've read, the CLR gives each App Domain a thread pool of 25 threads, and once this pool is exhausted then any new threads created with BeginInvoke will block until the pool frees up another thread. Am I right on that? I did a little test where I went into a loop and attempted to spawn 50 new worker threads with a call to BeginInvoke that used an asynchronous callback. I expected it to launch 24 threads, then block for...
18
2040
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate, consider what I'll do in Java: public interface DataAvailabilityListener extends java.util.EventListener { void dataArrived(DataAvailabilityEvent event); }
4
9813
by: Charles Law | last post by:
Suppose a worker thread needs to signal to the main thread that an event has occurred. Ordinarily, any event raised by the worker thread will be on its own thread. How can the worker thread raise an event on the main thread instead? TIA Charles
12
4143
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. However in vb.net you get continual messages (even setting the system modal property). Firstly, are these two assumptions right and if so what is the approved
3
2199
by: jrh | last post by:
Hi, I am relatively new at threads. I have a windows form and a class running on the same thread. The class has a background task that needs to be done every minute on a background thread. I am still deciding on whether to use System.Threading.Timer or Thread.Start/Thread.Sleep. The background task may then raise an event which will be captured by the windows form. I believe this event needs to be raised on the windows form thread. ...
9
2471
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
3
1834
by: =?Utf-8?B?THVib21pcg==?= | last post by:
Hi, I have an app with the main thread and one additional thread. In the main thread I defined an event handler MyHandler, the function for raisin this event OnMyEvent and the function for processing this event MyFunc. I want to raise this event from the second thread. Is it thread safe to call the method OnMyEvent from this second thread?
0
9591
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
10225
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
10053
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
8880
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
7415
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
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3969
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
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
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.