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

Async Delegates completion callback

I'm using an async delegate to spawn a thread and run some code. I also
specify an AsyncCallback to get notified when the thread completes. The
problem is that my callback is being called twice and I can't figure out
why. Here is a snippet of my code, the LoadImageThreadComplete() method is
being called twice even though LoadImage() is only ever called once. Any
ideas why?

delegate void LoadImageDelegate(string file);

private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
delg.BeginInvoke(file, new AsyncCallback(LoadImageThreadComplete),
null);
}

private void LoadImageThread(string file)
{
Thread.Threading.Sleep(1000);
}

private void LoadImageThreadComplete(IAsyncResult ar)
{
((AsyncCallback)ar.AsyncState).EndInvoke(ar);
}
Nov 16 '05 #1
4 2939
I recall some issue about this being related to the callback function
not using the _stdcall convention.

--
Xavier Pacheco
Xapware Technologies Inc

manage your projects: www.xapware.com/ActiveFocus.htm
the blog: www.xavierpacheco.com/xlog
the book: http://www.amazon.com/exec/obidos/AS...vierpacheco-20
Nov 16 '05 #2
You must call EndInvoke on the same delegate that initiated the call, one
option to do this is to pass the delegate as state object, like this:

private void LoadImageThread(string file)
{
Console.WriteLine("The call executed ");
System.Threading.Thread.Sleep(1000);
}
private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
IAsyncResult ar = delg.BeginInvoke(file, new
AsyncCallback(LoadImageThreadComplete),delg); // pass delg object as stae
object
}
private void LoadImageThreadComplete(IAsyncResult ar)
{
LoadImageDelegate dlgt = (LoadImageDelegate)ar.AsyncState; //use
stateobject and cast to the delegate that initiated the call
dlgt.EndInvoke(ar); // use this delegate to call EndInvoke
}

Willy.
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:eU*************@TK2MSFTNGP10.phx.gbl...
I'm using an async delegate to spawn a thread and run some code. I also
specify an AsyncCallback to get notified when the thread completes. The
problem is that my callback is being called twice and I can't figure out
why. Here is a snippet of my code, the LoadImageThreadComplete() method
is
being called twice even though LoadImage() is only ever called once. Any
ideas why?

delegate void LoadImageDelegate(string file);

private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
delg.BeginInvoke(file, new AsyncCallback(LoadImageThreadComplete),
null);
}

private void LoadImageThread(string file)
{
Thread.Threading.Sleep(1000);
}

private void LoadImageThreadComplete(IAsyncResult ar)
{
((AsyncCallback)ar.AsyncState).EndInvoke(ar);
}

Nov 16 '05 #3
Lets confirm or remove the possibility thay the threadpool is calling your callback twice .. you haven't given us the real code as can be ssen from your BeginInvoke call and the callback. You don;t pass anything in to the AsyncState in teh call to BeginOnvoke yet you miraculously pick up the delegate from the AsyncState member of IAsycResult in the callback.

Put calls that output in whatever mechanism works for you AppDomain.GetCurrentThreadId(); in each bit of the processing and when the callback is called see which thread is calling it.

Or post the real code (a cut down version that compiles and works).

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<eU*************@TK2MSFTNGP10.phx.gbl>

I'm using an async delegate to spawn a thread and run some code. I also
specify an AsyncCallback to get notified when the thread completes. The
problem is that my callback is being called twice and I can't figure out
why. Here is a snippet of my code, the LoadImageThreadComplete() method is
being called twice even though LoadImage() is only ever called once. Any
ideas why?

delegate void LoadImageDelegate(string file);

private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
delg.BeginInvoke(file, new AsyncCallback(LoadImageThreadComplete),
null);
}

private void LoadImageThread(string file)
{
Thread.Threading.Sleep(1000);
}

private void LoadImageThreadComplete(IAsyncResult ar)
{
((AsyncCallback)ar.AsyncState).EndInvoke(ar);
}

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #4
That did the trick. Thanks.

-Brett-

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
You must call EndInvoke on the same delegate that initiated the call, one
option to do this is to pass the delegate as state object, like this:

private void LoadImageThread(string file)
{
Console.WriteLine("The call executed ");
System.Threading.Thread.Sleep(1000);
}
private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
IAsyncResult ar = delg.BeginInvoke(file, new
AsyncCallback(LoadImageThreadComplete),delg); // pass delg object as stae
object
}
private void LoadImageThreadComplete(IAsyncResult ar)
{
LoadImageDelegate dlgt = (LoadImageDelegate)ar.AsyncState; //use
stateobject and cast to the delegate that initiated the call
dlgt.EndInvoke(ar); // use this delegate to call EndInvoke
}

Willy.
"Brett Robichaud" <br************@nospam.yahoo.com> wrote in message
news:eU*************@TK2MSFTNGP10.phx.gbl...
I'm using an async delegate to spawn a thread and run some code. I also
specify an AsyncCallback to get notified when the thread completes. The
problem is that my callback is being called twice and I can't figure out
why. Here is a snippet of my code, the LoadImageThreadComplete() method
is
being called twice even though LoadImage() is only ever called once. Any ideas why?

delegate void LoadImageDelegate(string file);

private void LoadImage(string file)
{
LoadImageDelegate delg = new LoadImageDelegate(LoadImageThread);
delg.BeginInvoke(file, new AsyncCallback(LoadImageThreadComplete),
null);
}

private void LoadImageThread(string file)
{
Thread.Threading.Sleep(1000);
}

private void LoadImageThreadComplete(IAsyncResult ar)
{
((AsyncCallback)ar.AsyncState).EndInvoke(ar);
}


Nov 16 '05 #5

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

Similar topics

3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
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...
5
by: Paul Hasell | last post by:
Hi, I'm trying to invoke a web method asynchronously but just can't seem to get it to tell me when it has finished! Below is the code I am (currently) using: private void...
4
by: Bob Badger | last post by:
Hi, Simple question (although I guess with a complicated answer). Is HTTP an async protocol? For instance, if I send a message to a c# webservice via http what is the protocol actually doing? ...
1
by: Demi | last post by:
I'm trying to use an Async="true" page to do an async HttpWebRequest. My code is based on the MSDN example: http://msdn2.microsoft.com/en-us/library/21k58ta7.aspx The problem I'm having is...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
3
by: Giulio Petrucci | last post by:
Hi there, I'm quite a newbie in Web Service programming/using and I'm sorry if my question could sound quite "stupid". ;-) I'm working on an application which should request a service to a...
13
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.