Home All Groups Group Topic Archive Search About

How to raise async events

Author
28 Nov 2007 11:45 AM
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

Author
28 Nov 2007 1:33 PM
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.

nadeem_***@yahoo.com wrote:
Show quote
> 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
Author
28 Nov 2007 4:06 PM
Ignacio Machin ( .NET/ C# MVP )
Hi,


--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<nadeem_***@yahoo.com> wrote in message
news:4471a130-48b7-40d4-a9c7-4946bc942997@i12g2000prf.googlegroups.com...
> Hello,

>
> 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.
Author
28 Nov 2007 5:19 PM
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


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

<nadeem_***@yahoo.com> wrote in message
Show quote
news:4471a130-48b7-40d4-a9c7-4946bc942997@i12g2000prf.googlegroups.com...
> 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
Author
28 Nov 2007 6:03 PM
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);
}

--
Chris Mullins

<nadeem_***@yahoo.com> wrote in message
Show quote
news:4471a130-48b7-40d4-a9c7-4946bc942997@i12g2000prf.googlegroups.com...
> 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
Author
28 Nov 2007 10:02 PM
Ben Voigt [C++ MVP]
Show quote
"Chris Mullins [MVP - C#]" <cmull***@yahoo.com> wrote in message
news:%23kDqmjeMIHA.5224@TK2MSFTNGP02.phx.gbl...
> 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....

Show quote
> }
>
> --
> Chris Mullins
>
> <nadeem_***@yahoo.com> wrote in message
> news:4471a130-48b7-40d4-a9c7-4946bc942997@i12g2000prf.googlegroups.com...
>> 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
>
>
Author
28 Nov 2007 10:20 PM
Chris Mullins [MVP - C#]
Show quote
"Ben Voigt [C++ MVP]" <rbv@nospam.nospam> wrote
> "Chris Mullins [MVP - C#]" <cmull***@yahoo.com> wrote in message

>>    // 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....

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

--
Chris Mullins
Author
29 Nov 2007 10:59 AM
nadeem_far
Show quote
On Nov 29, 6:20 am, "Chris Mullins [MVP - C#]" <cmull***@yahoo.com>
wrote:
> "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote
>
> > "Chris Mullins [MVP - C#]" <cmull***@yahoo.com> wrote in message
> >>    // We're on a threadpool thread now. When weraisethe 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....
>
> You're right, of course. That's what i get for posting when I'm tired...
>
> --
> Chris Mullins

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.

AddThis Social Bookmark Button