473,385 Members | 2,162 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,385 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 16 '05 #1
4 17243
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 16 '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 16 '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 16 '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 16 '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...
13
by: ALI-R | last post by:
Hi All, When we say events are processed asynchronously in for instance Sharepoint ,what dose it mean? Thanks for your help. Ali
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...
2
by: Tina | last post by:
I am trying to raise a custom event in a user control passing custom data arguments. I have done this many times in VB but this is my first time in C#. The code below passes the proper...
2
by: Tina | last post by:
(my last post was incomplete so this issue is being reposted) I have an ASP.Net C# app with a Default.aspx page with an ascx user control on the page named EditGrid.ascx given the ID myEG on the...
0
by: jimscafe | last post by:
I have a radiobox in wzPython which defaults to Selection(0) When a different selection is made with the mouse it raises an event wx.EVT_RADIOBOX - which calls a method I created def...
1
by: Franck | last post by:
Hi, Got a Form which makes call to Thomson DataStream DataBase using an asynchronous system. I launch this Form from a Main one and would like to know if it's possible to trap DataStream Form...
3
by: Smithers | last post by:
In consideration of the brief sample code at the following link... http://msdn2.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx .... when we set e.Cancel = true,...
4
Airslash
by: Airslash | last post by:
Hello, I have designed a basic wrapper around the UDP socket from the .NET framework, that resembles an UDP server. Whenever I recieve data, I wish to raise an asynchronous event. According the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...

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.