Thursday, August 04, 2005

Asynchronous programming in C#

Long time since I posted something technical. So here I will discuss about support for asynchronous programming in C#. Do post your thoughts on how much effort this could have taken in C++? Any C++ libraries which can do this?

.Net provides new simple ways to perform asynchronous programming. Asynchronous delegates provide the ability to invoke any method asynchronously. (A delegate is equivalent to a function pointer).

Method parameters can be used for input or output. (C++: &var pass by reference). C# refines this by introducing new keywords: out and ref.
out states that the parameter's value set the by the method. For example, I can define a function as:

public void Method(float param, out float multResult, out float divResult)
{
//...
}


and invoke it:
float multResult, divResult;
Method(100,out multResult,out divResult);
Console.Writeline(multResult);
Console.Writeline(divResult);

We will first see how we can invoke a method asynchronously. We will also answer a doubt which might arise: 'If I invoke 'Method' asynchronously, what will be the values of out params?'

public class AyncTest
{
//Define the delegate
public delegate void AsyncDelegate(int inParam,out int outParam);
private AsyncDelegate m_asyncDelegate;

public void Test()
{
m_asyncDelegate = new AsyncDelegate(AsyncMethod);

System.AsyncCallback asyncCallback = new AsyncCallback(OnAsyncReturn);

int outParam;

IAsyncResult result = m_asyncDelegate.BeginInvoke(2,out outParam,asyncCallback,null);

Console.Writeline(outParam) //<--- whats the result?

result.AsyncWaitHandle.WaitOne();
}

public void OnAsyncReturn(System.IAsyncResult result)
{
int outParam;
m_asyncDelegate.EndInvoke(out outParam,result);
Console.WriteLine("Result: "+outParam);
}

public void AsyncMethod(int inParam,out int outParam)
{
outParam = inParam * 100;
}
}

So as seen asynchronous invocation is quite simpler than, what you would have to do, without this kind of support.

And now to the question: 'If I invoke 'Method' asynchronously, what will be the values of out params?'. As you might have guessed the out params are set to default values, int, float set to zero, objects set to null.

24 Comments:

At 12:37 PM, Blogger Gops said...

Hey Narasimha,

By asynchronous methods, do you mean callbacks that execute when an event occurs? If that is the case, you can do it in C++ with functors and threads, or in C with function pointers and threads.

(Sorry, I didn't have the patience to go through the AsyncTest code)

 
At 11:07 PM, Blogger NGM said...

Hi Gopal,

My mistake, should never have put so much code :)

Well it is about this: How can i invoke a function asynchrnously, define a callback for the result which get invoked when the function completes its execution.

C# provides features faclity so that you do not have to declare a thread or an event, it does all this in the background

 
At 6:42 PM, Blogger Balbir Singh said...

Narshima,

What happens if the callback runs for too long? Does it impact other Asynchronous Delegates?

We do many such things in the Linux kernel for our drivers, but we need to be careful about impacting other people using the same thread for callbacks.

 
At 9:50 PM, Blogger NGM said...

Hello Balbir,

Each asynchronous delegate runs in a thread of its own, The execution of one I beleibe should not affect the execution of another.

But the CLR has limited set of threads which it can create.

But this should not matter as Asynchronous is usually the exception rather than normal

 
At 9:31 PM, Blogger Balbir Singh said...

Would it be correct to say it is a wrapper. I mean, I could achieve the same thing by creating my own thread

 
At 10:37 PM, Blogger NGM said...

Yes and it reduces that effort

 
At 9:28 PM, Anonymous Anonymous said...

Thank you!
[url=http://rjtjrepo.com/deys/jwhm.html]My homepage[/url] | [url=http://bugldlae.com/roqp/ohhf.html]Cool site[/url]

 
At 9:28 PM, Anonymous Anonymous said...

Great work!
My homepage | Please visit

 
At 9:29 PM, Anonymous Anonymous said...

Good design!
http://rjtjrepo.com/deys/jwhm.html | http://upnoqucn.com/lrcm/irhw.html

 
At 11:01 PM, Anonymous Anonymous said...

Hello!

Nice site, keep up the good work .

http://buy-phentermine.hem.nu BUY PHENTERMINE
BUY PHENTERMINE
http://blog.sol.no/buy-phentermine
BUY PHENTERMINE
http://buy-phentermine.hem.nu buy phentermine
http://buy-phentermine.hem.nu phentermine online
http://buy-phentermine.hem.nu order phentermine
http://buy-phentermine.hem.nu cheap phentermine
http://buy-phentermine.hem.nu buy phentermine online
http://buy-phentermine.hem.nu phentermine diet pill
http://buy-phentermine.hem.nu phentermine online pharmacy
http://buy-phentermine.hem.nu phentermine prescription
http://buy-phentermine.hem.nu what is phentermine
http://buy-phentermine.hem.nu free phentermine

 
At 5:57 AM, Anonymous Anonymous said...

Hello! Good site narasimhagm.blogspot.com! Thanks.
hydrocodone
http://www11.asphost4free.com/tramadolrx/Tramadol.html tramadol tramadol

 
At 7:34 PM, Anonymous Anonymous said...

Hello! Good page www.blogger.com! Thanks.
Meds adipex . Your pharmacist has more information about Adipex written for health professionals that you may read.
buy levitra adipex no prescription ivana fukalot
http://adipexnoprescription.vidiac.com
http://cumshots.bloggingmylife.com/
http://ivanafukalot.vidiac.com/
http://forcedsex.forum-on.de/
http://buylevitra.vidiac.com

 
At 8:55 PM, Anonymous Anonymous said...

Hi all!
very good site, thanks
erotic art buy valium order viagra beastiality brother sister sex cheap phentermine forced sex asian rape adipex no prescription
[url=http://myurl.com.tw/ncn3]erotic art[/url] [url=http://myurl.com.tw/ilsr]buy valium[/url] [url=http://myurl.com.tw/i94i]order viagra[/url] [url=http://beastiality.bloggingmylife.com/]beastiality[/url] [url=http://brother-sister-sex.zj.pl/]brother sister sex[/url] [url=http://miniurl.pl/cheapphentermine]cheap phentermine[/url] [url=http://miniurl.pl/forcedsex]forced sex[/url] [url=http://miniurl.pl/asianrape]asian rape[/url] [url=http://miniurl.pl/adipexrx]adipex no prescription[/url]
http://myurl.com.tw/ncn3 http://myurl.com.tw/ilsr http://myurl.com.tw/i94i http://beastiality.bloggingmylife.com/ http://brother-sister-sex.zj.pl/ http://miniurl.pl/cheapphentermine http://miniurl.pl/forcedsex http://miniurl.pl/asianrape http://miniurl.pl/adipexrx

 
At 3:39 AM, Anonymous Anonymous said...

Hello all
very good site www.blogger.com, thanks
Buy cheap adipex online! Buy cheap tramadol online! brother sister sex cartoon incest Buy cheapest cialis pharmacy daughter incest .
[url=http://www.volny.cz/ph24/cheap-adipex.html]cheap adipex[/url] [url=http://www.volny.cz/ph24/cheap-tramadol.html]cheap tramadol[/url] [url=http://www.volny.cz/ph24/porn/brother-sister-sex.html]brother sister sex[/url] [url=http://www.volny.cz/ph24/porn/cartoon-incest.html]cartoon incest[/url] [url=http://www.volny.cz/ph24/porn/daughter-incest.html]daughter incest[/url] [url=http://www.volny.cz/ph24/cheapest-cialis.html]cheapest cialis[/url]
http://www.volny.cz/ph24/cheap-adipex.html http://www.volny.cz/ph24/cheapest-cialis.html http://www.volny.cz/ph24/cheap-tramadol.html http://www.volny.cz/ph24/porn/brother-sister-sex.html http://www.volny.cz/ph24/porn/cartoon-incest.html http://www.volny.cz/ph24/porn/daughter-incest.html

 
At 10:07 PM, Anonymous Anonymous said...

Hello all
very good site www.blogger.com, thanks
http://www.volny.cz/ph24/cialis-drug.html cialis drug [url=http://www.volny.cz/ph24/cialis-drug.html]cialis drug[/url]
http://www.volny.cz/ph24/generic-levitra.html generic levitra [url=http://www.volny.cz/ph24/generic-levitra.html]generic levitra[/url]
http://www.volny.cz/ph24/porn/forced-sex.html forced sex [url=http://www.volny.cz/ph24/porn/forced-sex.html]forced sex[/url]
http://www.volny.cz/ph24/porn/family-incest.html family incest [url=http://www.volny.cz/ph24/porn/family-incest.html]family incest[/url]
http://www.volny.cz/ph24/phentermine-37.5-mg.html phentermine 37.5 mg [url=http://www.volny.cz/ph24/phentermine-37.5-mg.html]phentermine 37.5 mg[/url]
http://www.volny.cz/ph24/porn/hardcore-fucking.html hardcore fucking [url=http://www.volny.cz/ph24/porn/hardcore-fucking.html]hardcore fucking[/url]

 
At 4:53 AM, Anonymous Anonymous said...

Hello!

Nice site, keep up the good work .

http://buy-phentermine.hem.nu BUY PHENTERMINE
BUY PHENTERMINE
http://blogg.sol.no/buy-phentermine
BUY PHENTERMINE

http://lesbian-school.moviesparisexposed.info/ LESBIAN SCHOOL
http://hot-school-teacher-naked.moviesparisexposed.info/ HOT SCHOOL TEACHER NAKED
http://sex-personals.moviesparisexposed.info/ SEX PERSONALS
http://virgin-blowjob.moviesparisexposed.info/ VIRGIN BLOWJOB
http://rabbit-vibrator.moviesparisexposed.info/ RABBIT VIBRATOR
http://college-group-orgy.moviesparisexposed.info/ COLLEGE GROUP ORGY
http://horny-school-teachers.moviesparisexposed.info/ HORNY SCHOOL TEACHERS
http://video-strip-poker.moviesparisexposed.info/ VIDEO STRIP POKER
http://adult-incontinence-product.moviesparisexposed.info/ ADULT INCONTINENCE PRODUCT
http://gothic-blowjob.moviesparisexposed.info/ GOTHIC BLOWJOB
http://blowjob-cartoon.moviesparisexposed.info/ BLOWJOB CARTOON
http://school-naked.moviesparisexposed.info/ SCHOOL NAKED
http://college-with-big-tits.moviesparisexposed.info/ COLLEGE WITH BIG TITS
http://bikini-blowjob.moviesparisexposed.info/ BIKINI BLOWJOB
http://college-fuck-fest-clips.moviesparisexposed.info/ COLLEGE FUCK FEST CLIPS
http://teen-masturbation-education.moviesparisexposed.info/ TEEN MASTURBATION
http://after-school-sex.moviesparisexposed.info/ AFTER SCHOOL SEX
http://college-hotties.moviesparisexposed.info/ COLLEGE HOTTIES

 
At 8:11 AM, Anonymous Anonymous said...

Hello!
very good site www.blogger.com, thanks
http://uscialis.forumppl.com/ cheapest cialis cheap cialis buy cialis [url=http://uscialis.forumppl.com/]cheapest cialis[/url] [url=http://uscialis.forumppl.com/]buy cialis[/url]
http://blogyaz.com/viagraalter/ viagra alternative [url=http://blogyaz.com/viagraalter/]viagra alternative[/url]
http://blogyaz.com/incest/ incest videos [url=http://blogyaz.com/incest/]incest porn[/url]
http://incest.forumppl.com/ incest incest incest videos [url=http://incest.forumppl.com/]incest[/url] [url=http://incest.forumppl.com/]incest videos[/url]
http://incest.forum-on.de incest porn incest incest porn [url=http://incest.forum-on.de]incest porn[/url] [url=http://incest.forum-on.de]incest videos[/url]
http://cheaphentermine.phpbbx.de/ cheap phentermine phentermine phentermine [url=http://cheaphentermine.phpbbx.de/]cheap phentermine[/url] [url=http://cheaphentermine.phpbbx.de/]phentermine[/url]

 
At 8:44 AM, Anonymous Anonymous said...

Hi!
Good design on www.blogger.com, great info.
Best online drugstore. Buy adipex meridia propecia hydrocodone:
http://daniell.hostuju.cz/adipex.html adipex [url=http://daniell.hostuju.cz/adipex.html]adipex[/url]
http://daniell.hostuju.cz/buy-hydrocodone.html buy hydrocodone [url=http://daniell.hostuju.cz/buy-hydrocodone.html]buy hydrocodone[/url]
http://209384.hostuju.cz/brother-sister-sex.html brother sister sex [url=http://209384.hostuju.cz/brother-sister-sex.html]brother sister sex[/url]
http://209384.hostuju.cz/hardcore-fucking.html hardcore fucking [url=http://209384.hostuju.cz/hardcore-fucking.html]hardcore fucking[/url]
http://daniell.hostuju.cz/buy-propecia.html buy propecia [url=http://daniell.hostuju.cz/buy-propecia.html]buy propecia[/url]
http://209384.hostuju.cz/russian-rape.html russian rape [url=http://209384.hostuju.cz/russian-rape.html]russian rape[/url]

 
At 2:48 AM, Anonymous Anonymous said...

Hello!
Nice site in web www.blogger.com!
Cheap drugstore online. Buy coumadin meridia propecia:
http://daniell.hostuju.cz/coumadin.html coumadin [url=http://daniell.hostuju.cz/coumadin.html]coumadin[/url] http://209384.hostuju.cz/asian-rape.html asian rape [url=http://209384.hostuju.cz/asian-rape.html]asi
an rape[/url] http://209384.hostuju.cz/cartoon-incest.html cartoon incest [url=http://209384.hostuju.cz/cartoon-incest.html]cartoon incest[/url] http://209384.hostuju.cz/incest-porn.html incest porn [url=http://209384.hostuju.cz/incest-porn.html]incest porn[/url] http://users.nethit.pl/cgi-bin/vote/vote?tramadolhcl tramadol hcl [url=http://users.nethit.pl/cgi-bin/vote/vote?tramadolhcl]tramadol hcl[/url] http://
209384.hostuju.cz/gay-incest.html gay incest [url=http://209384.hostuju.cz/gay-incest.html]gay incest[/url]

 
At 2:32 AM, Anonymous Anonymous said...

Hello!
Good site www.blogger.com, thanks.
Good Drugstore Online. Buy tramadol cialis adipex:
http://cheapescialis.blogcu.com/ buy cheapest cialis [url=http://cheapescialis.blogcu.com/]buy cheapest cialis[/url] http://www.forumup.us/?mforum=beastiality beastiality [url=http://www.forumup.us/?mforum=beastiality]beastiality[/url] http://ivanafuka
lot.forumup.us/ ivana fukalot [url=http://ivanafukalot.forumup.us/]ivana fukalot[/url] http://incestxxx.mybbland.com family incest porn [url=http://incestxxx.mybbland.com]family incest porn[/url]

 
At 9:30 PM, Anonymous Anonymous said...

Hello!
Good design on www.blogger.com, good info.
Buy adipex pills
Buy adipex http://site.voila.fr/usmeds/buy_adipex.html buy adipex [url=http://site.voila.fr/usmeds/buy_adipex.html]buy adipex[/url] http://rape-v
ideos.col.nu/ rape videos [url=http://rape-videos.co

 
At 5:48 PM, Anonymous Anonymous said...

HI! Your forum is very interessing and my information will be good for you (I hope):)
[URL=http://anal-sex-movies.domaintraf.com]anal sex movies[/URL]
[URL=http://gaping-anal-sex.trafroute.com]gaping anal sex[/URL]
[URL=http://extreme-anal-sex.kissthisass.com]extreme anal sex[/URL]
[URL=http://anal-painful-sex.privateinn.com]anal painful sex[/URL]
[URL=http://anal-porn-sex.guaero.com]anal porn sex[/URL]
[URL=http://galleries-anal-sex.domaintraf.com]galleries anal sex[/URL

 
At 7:00 PM, Anonymous Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服酒店

酒店兼差

酒店上班

酒店PT

酒店

酒店喝酒

酒店消費

喝花酒

粉味

喝酒

 
At 4:09 PM, Anonymous Anonymous said...

酒店經紀酒店打工 酒店工作酒店上班 酒店兼差 酒店兼職打工兼差打工兼職台北酒店酒店應徵 禮服酒店 酒店 經紀 打工 兼差

 

Post a Comment

<< Home