How to raise async events

N

nadeem_far

Hello,

I am working on a c# project using framework 1.1.
Since its a console application,no windows forms are being used.
I am trying to implement event driven classes that just want to raise
the event and continue working( like fire and forget - do not wait for
the event handler in client to finish.)

I have gone through alot of the articles on internet and MSDN and most
talk about delegates (or BackGroundWorker in 2.0 which I cannot
use.) all the other solutions out there are for classes raisng async
events for windows forms in one way or the other.

I am interested in a solution that allows two classes that do not
inherit from Control class and raise events and work togather in async
fashion.

I will really appreciate if some one can help me with this.

Thanks,

Nad
 
O

octoberclub

You could use System.Threading.Thread class to start another method in
a new thread. You can raise an event as normal from this thread using
a delegate.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
I am interested in a solution that allows two classes that do not
inherit from Control class and raise events and work togather in async
fashion.

IIRC there were an article a time ago in MSDN magazine about using events in
a console app. I do remember it was one of the last article in the number.
The front page fo the number was related to C# and LINQ. So it will be easy
to find.
 
N

Nicholas Paldino [.NET/C# MVP]

Nad,

In this case, instead of just invoking the delegate, you will want to
get the invocation list and then call BeginInvoke on each of the items in
the invocation list. This will cause the event to be fired on a thread from
the thread pool.

Of course, this means that your subscribers have to be aware of the fact
that the events they subscribe to are on another thread.

Juval Lowy has created an interesting little class called EventsHelper
which will do this for you:

http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=-1&tabid=19&download=98

You can find more information about EventsHelper here:

http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11
 
C

Chris Mullins [MVP - C#]

It's really pretty easy to do. The code below probably won't compile, as I
just wrote it here in the message, but it should be pretty close.

As a caviet, you're in for a world of hurt, in terms of needing to go
through the concurrency learning curve. As soon as you have multiple threads
running at once (which this code does) then concurrency shows up...

public event GuidEvent;

public void DoingSomething()
{
string value = Guid.NewGuid().ToString();
Console.WriteLine("It's time to trigger the Async Event");

ThreadPool.QueueUserWorkItem(AsyncGuidEvent, value)

Console.WriteLine("Event Triggered. This thread still going...");
}

private void AsyncGuidEvent(object o)
{
// We're on a threadpool thread now. When we raise the event,
// the people listening will be called on THIS thread.
string s = (string)o;
if (this.GuidEvent != null)
GuidEvent(s);
}
 
B

Ben Voigt [C++ MVP]

Chris Mullins said:
It's really pretty easy to do. The code below probably won't compile, as I
just wrote it here in the message, but it should be pretty close.

As a caviet, you're in for a world of hurt, in terms of needing to go
through the concurrency learning curve. As soon as you have multiple
threads running at once (which this code does) then concurrency shows
up...

public event GuidEvent;

public void DoingSomething()
{
string value = Guid.NewGuid().ToString();
Console.WriteLine("It's time to trigger the Async Event");

ThreadPool.QueueUserWorkItem(AsyncGuidEvent, value)

Console.WriteLine("Event Triggered. This thread still going...");
}

private void AsyncGuidEvent(object o)
{
// We're on a threadpool thread now. When we raise the event,
// the people listening will be called on THIS thread.
string s = (string)o;
if (this.GuidEvent != null)
GuidEvent(s);

For shame, Chris! You're a concurrency expert.

var localGuidEvent = this.GuidEvent;
if (localGuidEvent != null)
localGuidEvent(s);

Otherwise you must be prepared to catch a NullReferenceException if
GuidEvent is changed during the execution of AsyncGuidEvent. But then,
that's the world of hurt mentioned at the beginning of your reply....
 
C

Chris Mullins [MVP - C#]

For shame, Chris! You're a concurrency expert.

var localGuidEvent = this.GuidEvent;
if (localGuidEvent != null)
localGuidEvent(s);

Otherwise you must be prepared to catch a NullReferenceException if
GuidEvent is changed during the execution of AsyncGuidEvent. But then,
that's the world of hurt mentioned at the beginning of your reply....

You're right, of course. That's what i get for posting when I'm tired...
 
N

nadeem_far

You're right, of course. That's what i get for posting when I'm tired...

thanks guys for your help.

Are there any good examples that I can try out? The link that Nicholas
gave is not working. the other one is for downloading.
Also will appreciate if you can guide me to some good articles on this
topic.

Thanks,

Nad.
 
N

nadeem_far

thanks guys for your help.

Are there any good examples that I can try out? The link that Nicholas
gave is not working. the other one is for downloading.
Also will appreciate if you can guide me to some good articles on this
topic.

Thanks,

Nad.- Hide quoted text -

- Show quoted text -

Guys,

I checked out the Eventhelper. The implementation is for ,NET 2.0. I
am looking for something which is for 1.1

Thanks.

Nad
 
N

nadeem_far

For shame, Chris! You're a concurrency expert.

var localGuidEvent = this.GuidEvent;
if (localGuidEvent != null)
localGuidEvent(s);

Otherwise you must be prepared to catch a NullReferenceException if
GuidEvent is changed during the execution of AsyncGuidEvent. But then,
that's the world of hurt mentioned at the beginning of your reply....




- Show quoted text -- Hide quoted text -

- Show quoted text -

Guys,

You said when the event will be raised it will be on a new thread. Now
can you tell me how can I know from the event handler that
this is on a different thread and pass the call to the actual thread
on which the eventhandler would be called in sync mode?

Thanks,

Nad
 
P

Peter Duniho

You said when the event will be raised it will be on a new thread. Now
can you tell me how can I know from the event handler that
this is on a different thread and pass the call to the actual thread
on which the eventhandler would be called in sync mode?

Well, the first question you should ask is whether that's actually
necessary. In a forms application, you would need to do so _in certain
situations_ because calls to access, modify, interact with, etc. a Control
instance have to be made on the same thread where that Control was
created. But you said you're doing a console application and so that
requirement shouldn't exist. Usually it would be sufficient to just
synchronize access to shared data structures.

For that matter, even in a forms application that would often be a
reasonable solution, as long as the work being done didn't involve
interaction with the UI elements of the application.

If you do truly have a need for the event being raised to be handled on
some other thread, you'll have to implement that logic yourself. A simple
mechanism might involve a queue of delegates that the other thread
consumes. Threads wanting to executed code on that other thread would
create a delegate for the code they want executed and put the delegate in
the queue.

Hope that helps.

Pete
 
N

nadeem_far

Well, the first question you should ask is whether that's actually
necessary. In a forms application, you would need to do so _in certain
situations_ because calls to access, modify, interact with, etc. a Control
instance have to be made on the same thread where that Control was
created. But you said you're doing a console application and so that
requirement shouldn't exist. Usually it would be sufficient to just
synchronize access to shared data structures.

For that matter, even in a forms application that would often be a
reasonable solution, as long as the work being done didn't involve
interaction with the UI elements of the application.

If you do truly have a need for the event being raised to be handled on
some other thread, you'll have to implement that logic yourself. A simple
mechanism might involve a queue of delegates that the other thread
consumes. Threads wanting to executed code on that other thread would
create a delegate for the code they want executed and put the delegate in
the queue.

Hope that helps.

Pete

Thanks all,

With your helping pointers I have been able to create classes that
communicate using events
with out inheriting from Control class.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top