473,407 Members | 2,315 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,407 software developers and data experts.

Asynchronous Event Raising

Hi all,
How Can You Raise Events Asynchronously ?
Now for the details ...

I want to do inter modular communication using events in such a way that the
contributing modules need not maintain the reference to any module.

For e.g. There is one EventManager class that has event declaration and
public functions that raise events. So any other class that needs to publish
data to interested classes, can call the event raiser method of the
EventManager class. The interested classes would already by handling the
event and will get the notification/data.

Public Class EventManager

Public Shared Event m_Notification()

Public Shared Sub NotificationRaiser()

RaiseEvent m_Notification()

End Sub

End Class
Thats fine and working. Now the problem is that the Event raising mechanism
in .NET is synchronous so in above example the control wont leave the
NotificationRaiser until all classes that handle the notification have done
their task. I would like this to be on separate thread (as much
automatically as possible - dont wanna do the plumbing myself).

So is there any way to raise some event asynchronously (like on some other
thread) without the reference of the class that is handling the event.

I hope I made myself clear, if there are still any queries .. then plz lemme
know.

Any suggestion or reference is welcome.

Thanx
rawCoder
Nov 21 '05 #1
4 4432
Just to make myself a lil more clear ....
Want it like it was in Win32 PostMessage ...
Fire And Forget ...
"rawCoder" <ra******@hotmail.com> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
Hi all,
How Can You Raise Events Asynchronously ?
Now for the details ...

I want to do inter modular communication using events in such a way that the contributing modules need not maintain the reference to any module.

For e.g. There is one EventManager class that has event declaration and
public functions that raise events. So any other class that needs to publish data to interested classes, can call the event raiser method of the
EventManager class. The interested classes would already by handling the
event and will get the notification/data.

Public Class EventManager

Public Shared Event m_Notification()

Public Shared Sub NotificationRaiser()

RaiseEvent m_Notification()

End Sub

End Class
Thats fine and working. Now the problem is that the Event raising mechanism in .NET is synchronous so in above example the control wont leave the
NotificationRaiser until all classes that handle the notification have done their task. I would like this to be on separate thread (as much
automatically as possible - dont wanna do the plumbing myself).

So is there any way to raise some event asynchronously (like on some other
thread) without the reference of the class that is handling the event.

I hope I made myself clear, if there are still any queries .. then plz lemme know.

Any suggestion or reference is welcome.

Thanx
rawCoder

Nov 21 '05 #2
rawCoder,

If you have a reference to a delegate list (which is what your event is
declared at, ultimately), then all you have to do is call the BeginInvoke
method, and it will be fired on another thread. However, the event handlers
have to code against the fact that the event notification will be on another
thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"rawCoder" <ra******@hotmail.com> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
Hi all,
How Can You Raise Events Asynchronously ?
Now for the details ...

I want to do inter modular communication using events in such a way that
the
contributing modules need not maintain the reference to any module.

For e.g. There is one EventManager class that has event declaration and
public functions that raise events. So any other class that needs to
publish
data to interested classes, can call the event raiser method of the
EventManager class. The interested classes would already by handling the
event and will get the notification/data.

Public Class EventManager

Public Shared Event m_Notification()

Public Shared Sub NotificationRaiser()

RaiseEvent m_Notification()

End Sub

End Class
Thats fine and working. Now the problem is that the Event raising
mechanism
in .NET is synchronous so in above example the control wont leave the
NotificationRaiser until all classes that handle the notification have
done
their task. I would like this to be on separate thread (as much
automatically as possible - dont wanna do the plumbing myself).

So is there any way to raise some event asynchronously (like on some other
thread) without the reference of the class that is handling the event.

I hope I made myself clear, if there are still any queries .. then plz
lemme
know.

Any suggestion or reference is welcome.

Thanx
rawCoder

Nov 21 '05 #3
BeginInvoke won't work directly because it can only be called on a
delegate with a single subscriber and will throw an exception if you
have multiple subscribers.

Also if you use BeginInvoke you must call EndInvoke.

Take a look at the following AsyncHelper Class.

The AsyncHelper.FireAndForget static method allows you to call a
delegate with a subscriber asynchronously without calling BeginInvoke

The AsyncHelper.FireAsync allows you to call a delegate with multiple
subscribers.

// Based on AsyncHelper by Mike Woodring of
http://staff.develop.com/woodring
//
// Use FireAndForget to execute a method in the thread pool without
having to
// call EndInvoke. Note that the delegate passed to FireAndForget can
only have a
// single Target.
// AsyncHelper.FireAndForget(new MyHandler(MyMethod), methodArg1,
methodArg2, etc);
//
// To Fire an Event asynchronously (with each of the targets being
called on its own thread
// use FireAsync:
// AsyncHelper.FireAsync(MyEvent, eventArg1, eventArg2, ...);
// FireAsync is designed to be called on an event containing zero, one
or more Targets.
// Your FireEvent method does not need to check for null FireAsync will
do it correctly even in a
// multithreaded environment
#region Using Statements
using System;
using System.Reflection;
using System.Threading;
using System.ComponentModel;
#endregion

namespace DanGo.Utilities
{
public class AsyncHelper
{
#region Private Types
// Private class holds data for a delegate to be run on the
thread pool
private class Target
{
#region Private Fields
private readonly Delegate TargetDelegate;
private readonly object[] Args;
#endregion

#region Constructor
/// <summary>
/// Creates a new <see cref="Target"/> instance this holds
arguments and contains
/// the method ExecuteDelegate to be called on the
threadpool.
/// </summary>
/// <param name="d">The users delegate to fire</param>
/// <param name="args">The users arguments to the
delegate</param>
public Target( Delegate d, object[] args)
{
TargetDelegate = d;
Args = args;
}
#endregion

#region Invoker
/// <summary>
/// Executes the delegate by calling DynamicInvoke.
/// </summary>
/// <param name="o">This parameter is required by the
threadpool but is unused.</param>
public void ExecuteDelegate( object o )
{
TargetDelegate.DynamicInvoke(Args);
}
#endregion
}
#endregion

#region Public Static Methods
/// <summary>
/// Fires the delegate without any need to call EndInvoke.
/// </summary>
/// <param name="d">Target Delegate - must contain only one
Target method</param>
/// <param name="args">Users arguments.</param>
public static void FireAndForget( Delegate d, params object[]
args)
{
Target target = new Target(d, args);
ThreadPool.QueueUserWorkItem(new
WaitCallback(target.ExecuteDelegate));
}

/// <summary>
/// Fires each of the members in the delegate asynchronously.
All the members
/// will be fired even if one of them fires an exception
/// </summary>
/// <param name="del">The delegate we want to fire</param>
/// <param name="args">Each of the args we want to
fire.</param>
public static void FireAsync(Delegate del, params object[]
args)
{
// copy the delegate to ensure that we can test for null in
a thread
// safe manner.
Delegate temp = del;
if(temp != null)
{
Delegate[] delegates = temp.GetInvocationList();
foreach(Delegate receiver in delegates)
{
FireAndForget(receiver, args);
}
}
}
#endregion
}
}

Nov 21 '05 #4
Hi raw (or do you prefer Mr Coder?)

It depends on what your observer classes are doing. If they are updating the
screen, for example, you could use BeginInvoke, which will return
immediately.

Otherwise, your observer could start another thread to process the event,
allowing it to return control straight away. Of course, this becomes more
tricky if another notification event is raised before the first one has been
processed, but perhaps then you can afford to wait.

HTH

Charles
"rawCoder" <ra******@hotmail.com> wrote in message
news:uT**************@tk2msftngp13.phx.gbl...
Hi all,
How Can You Raise Events Asynchronously ?
Now for the details ...

I want to do inter modular communication using events in such a way that
the
contributing modules need not maintain the reference to any module.

For e.g. There is one EventManager class that has event declaration and
public functions that raise events. So any other class that needs to
publish
data to interested classes, can call the event raiser method of the
EventManager class. The interested classes would already by handling the
event and will get the notification/data.

Public Class EventManager

Public Shared Event m_Notification()

Public Shared Sub NotificationRaiser()

RaiseEvent m_Notification()

End Sub

End Class
Thats fine and working. Now the problem is that the Event raising
mechanism
in .NET is synchronous so in above example the control wont leave the
NotificationRaiser until all classes that handle the notification have
done
their task. I would like this to be on separate thread (as much
automatically as possible - dont wanna do the plumbing myself).

So is there any way to raise some event asynchronously (like on some other
thread) without the reference of the class that is handling the event.

I hope I made myself clear, if there are still any queries .. then plz
lemme
know.

Any suggestion or reference is welcome.

Thanx
rawCoder

Nov 21 '05 #5

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

Similar topics

1
by: Bob1739 | last post by:
I have an Windows form app which receives data thru an external app. An event is raised in my app, some event handler code takes the data an puts it into a dataset. The dataset is bound to a...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
4
by: rawCoder | last post by:
Hi all, How Can You Raise Events Asynchronously ? Now for the details ... I want to do inter modular communication using events in such a way that the contributing modules need not...
8
by: Trotsky | last post by:
Hi I have asked a similar question on the web services discussion group, but the question is a bit more related to ASP.Net. Basically I have a ASP.Net application that calls a web service...
2
by: TrtnJohn | last post by:
I have a multi-threaded class that I am creating that needs needs to raise events when certain asynchronous events occur. I would always like the events to be raised to the primary UI thread of...
8
by: Paul Rubin | last post by:
I'd like to suggest adding a builtin abstract class to Python called AsynchronousException, which would be a subclass of Exception. The only asynchronous exception I can think of right now is...
3
by: senfo | last post by:
I recently read an MSDN article by Jeff Prosise titled, Scalable Apps with Asynchronous Programming in ASP.NET (http://msdn.microsoft.com/msdnmag/issues/07/03/WickedCode/). In the article, Jeff...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
4
by: Morgan Cheng | last post by:
Since ASP.NET 2.0, asynchronous web service client can be implemented with event-based pattern, instead of original BeginXXX/EndXXX pattern. However, I didn't find any material about event-based...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
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...
0
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,...
0
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...

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.