Canceling an asynchronous delegate

  • Thread starter Thread starter Diego Diaz
  • Start date Start date
D

Diego Diaz

Hello,

I'm facing the situation in which I need to cancel the
execution of a delegate that was started asynchronously
(through BeginInvoke and EndInvoke), but there is
no "CancelInvoke" or something like that. I noticed that
the interface for executing WebServices asynchronously
does have a method for this purpose, but unfortunately
there is a bug in .NET executing WebServices
asynchronously, so I had to wrap my proxy class with an
object so I can execute the WebService synchronously, but
in a different thread (through a local delegate);
however, now the problem is that I can't cancel it in a
safe manner (if I just ignore the running delegate,
subsequent executions of the proxy might start failing).

What do I need to do in order to cancel the execution of
an asynchronous delegate? Is it enough just to
call "Close" on the IAsyncResult pointer?

Thanks a lot in advance,

Diego Diaz
 
Hello Diego,

Thanks for posting in the group.

Aysnc. delegates use the thread pool, which provides no interface to cancel pending or executing requests. So there is no
way to abort an async call made through a delegate. Sometimes, programmers may suggest: "the results of the async call,
when it completes are held by the IAsyncResult objects returned by BeginInvoke. if you just let the IAsyncResult object to
go out of scope, it will be garbage collected and the results of the call will go away. The call will still complete, however."
This method is not recommended because it may break in the future and leak memory.

You need to either use your own thread and abort it when you need to kill the operation prematurely, or set some global flag
that the asysnc. delegate checks, and returns early if the flag is set.

Hope that helps.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Diego Diaz" <[email protected]>
!Sender: "Diego Diaz" <[email protected]>
!Subject: Canceling an asynchronous delegate
!Date: Mon, 18 Aug 2003 21:32:57 -0700
!Lines: 23
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNmCvclOxLKq7nETOCbTK+27h+zvw==
!Newsgroups: microsoft.public.dotnet.framework
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:51569
!NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Hello,
!
!I'm facing the situation in which I need to cancel the
!execution of a delegate that was started asynchronously
!(through BeginInvoke and EndInvoke), but there is
!no "CancelInvoke" or something like that. I noticed that
!the interface for executing WebServices asynchronously
!does have a method for this purpose, but unfortunately
!there is a bug in .NET executing WebServices
!asynchronously, so I had to wrap my proxy class with an
!object so I can execute the WebService synchronously, but
!in a different thread (through a local delegate);
!however, now the problem is that I can't cancel it in a
!safe manner (if I just ignore the running delegate,
!subsequent executions of the proxy might start failing).
!
!What do I need to do in order to cancel the execution of
!an asynchronous delegate? Is it enough just to
!call "Close" on the IAsyncResult pointer?
!
!Thanks a lot in advance,
!
!Diego Diaz
!
 
Back
Top