Killing Asynchronous Calls

Hi, I have thee following code that uses a delegate to make an asynchronous call. What I would like is a way of killing this call if need be. That is, the user could click on an abort button while the call is in progress, I want to then end that process immediately. Please can anyone help?

//instantiate delegate and make call

AsyncDelegate dlgt =new AsyncDelegate(objModel.CreateMDCFiles);

IAsyncResult ar = dlgt.BeginInvoke(null,null);

// Poll until call is finished.

while(!ar.IsCompleted)

{

Application.DoEvents();

}

Thanks in advance

[965 byte] By [GBez] at [2008-3-7]
# 1
The best that I can come up with is that the thread would be in the ThreadPool. I do not know if there is a way to tie up the returned WaitHandle that the IAsyncResult has with the appropriate thread in the ThreadPool.
However (I am assuming that dlgt is your dialog UI) you probably do not want to kill your UI thread.
I am thinking a more suitable option might be to break the code up such that whatever intensive BeginInvoke code that is being executed is not being executed on what I am assuming is your UI Thread. Instead spawning it on your own thread and keeping track of the thread yourself, thus allowing you to abort the thread when necessary.
MarcD at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 2
Cancellation has at least two different aspects - I/O cancellation, and cancelling CPU-bound tasks. Windows Vista is introducing API's for cancelling IO requests. You can use CancelSynchronousIoEx to cancel another thread's IO operations (with great care), and there's a separate API for cancelling async IO operations. IO cancellation is very tricky to do yourself, and we didn't expose enough surface area for you to easily build your own cancellation for async IO requests. For CPU-bound tasks that you want to cancel, the best way to do this is by polling for cancellation in your CPU-bound task. I would avoid aborting threads via Thread.Abort, or you'll need to start worrying about an escalation policy to guarantee you don't corrupt state.

If you go down this route and you want to support cancellation on threadpool threads or threads that process multiple items (instead of devoting a new thread to processing exactly one work item), you'll want a latched state variable shared between your watchdog & worker thread to indicate that a block of code should be cancelled, and you'll also want your cancellable code to define when it can and can't be cancelled. This is necessary to scope the cancellation to just the work item that you want to be cancelled.

We do not yet have a complete cancellation mechanism built into the .NET Framework. We have a very good idea about how we want to design it, but we want to make sure we get the cancellation model really correct & integrated with Indigo (Windows Communication Framework) in some manner. We also have System.Transactions with some related functionality. While I even checked in an implementation, it wasn't clear that what we would have been locking ourselves into was the correct model once all these other technologies were fully developed & in use. But you may want to keep an eye out for CancellationRegion & CancellationSignal classes, as well as an ICancelableAsyncResult in future versions.

Brian Grunkemeyer
MS CLR Base Class Library team

BrianGrunkemeyer at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...
# 3

I had similar problem. I wanted to send bytes from file to serial port using asynchronous call and allow the user to cancel the sending operation. What I did was I sent the the file in parts (making asynchronous call for every part).

What about throwing an exception until call is finished?

Sthrudel at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Remoting and Runtime Serialization...

.NET Development

Site Classified