473,378 Members | 1,417 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,378 software developers and data experts.

How to raise async events

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
Nov 28 '07 #1
11 3994
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:
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
Nov 28 '07 #2
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<na********@yahoo.comwrote in message
news:44**********************************@i12g2000 prf.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.
Nov 28 '07 #3
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/Deskt...19&download=98

You can find more information about EventsHelper here:

http://www.idesign.net/idesign/Deskt...dex=5&tabid=11
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<na********@yahoo.comwrote in message
news:44**********************************@i12g2000 prf.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

Nov 28 '07 #4
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

<na********@yahoo.comwrote in message
news:44**********************************@i12g2000 prf.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

Nov 28 '07 #5

"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote in message
news:%2****************@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....
}

--
Chris Mullins

<na********@yahoo.comwrote in message
news:44**********************************@i12g2000 prf.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


Nov 28 '07 #6
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote
"Chris Mullins [MVP - C#]" <cm******@yahoo.comwrote 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
Nov 28 '07 #7
On Nov 29, 6:20 am, "Chris Mullins [MVP - C#]" <cmull...@yahoo.com>
wrote:
"Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote
"Chris Mullins [MVP - C#]" <cmull...@yahoo.comwrote 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.
Nov 29 '07 #8
On Nov 29, 6:59 pm, nadeem_...@yahoo.com wrote:
On Nov 29, 6:20 am, "Chris Mullins [MVP - C#]" <cmull...@yahoo.com>
wrote:


"Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote
"Chris Mullins [MVP - C#]" <cmull...@yahoo.comwrote 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.- 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
Nov 30 '07 #9
On Nov 29, 6:02 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"Chris Mullins [MVP - C#]" <cmull...@yahoo.comwrote in messagenews:%2****************@TK2MSFTNGP02.phx.gb l...


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 theAsyncEvent");
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 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....
}
--
Chris Mullins
<nadeem_...@yahoo.comwrote in message
news:44**********************************@i12g2000 prf.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 toraise
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 raisngasync
eventsfor 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 andraiseeventsand work togather inasync
fashion.
I will really appreciate if some one can help me with this.
Thanks,
Nad- Hide quoted text -

- 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
Dec 1 '07 #10
On Sat, 01 Dec 2007 03:10:40 -0800, <na********@yahoo.comwrote:
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
Dec 1 '07 #11
On Dec 2, 4:26 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sat, 01 Dec 2007 03:10:40 -0800, <nadeem_...@yahoo.comwrote:
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
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.
Dec 10 '07 #12

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

Similar topics

1
by: Morgan Leppink | last post by:
Hi all - We have been developing a complex TCP socket communication app that is responsible for keeping numerous connections open to clients on routable IPs. The app receives request on a...
6
by: Vanessa | last post by:
I have a question regarding async mode for calling Microsoft.XMLHTTP object. Microsoft.XMLHTTP hangs the IE once in a while suddenly, but it will work again after half an hour or so without doing...
2
by: Andrew | last post by:
Hi, friends, I need to raise certain events in my VC# windows control library. Any reference paper or sample source code for help? Thanks a lot...
8
by: MuZZy | last post by:
Hi, Could someone pls help me here: If i use async sockets in the separate thread like this: void ThreadFunction() { ..... 1. MySocket.BeginAccept(AsyncCallBack(OnConnectRequest),...
0
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginXXX and EndXXX, plus the regular XXX syncronous method. Delegates seemed like the way to go, however, I still am having problems...
10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
2
by: IcedCrow | last post by:
Subject says it all. I want to raise an event in Sub New of a class but it is not being raised to my client app. I can raise events just fine in other procedures... just not sub new. Why is...
2
by: Pietro | last post by:
Hello, somebody know how to raise an event from a nested class? I have two classes, the class1 with 1 events, and a nested class (class2) inside the class1. So... How can I raise class1 events...
4
by: Marcolino | last post by:
Hi All, I'm using this code provided by Michael to run Async process: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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...

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.