By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,081 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,081 IT Pros & Developers. It's quick & easy.

Async Delegates completion callback

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.