Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel an asynchronous call?

How to cancel an asynchronous call? The .NET APM doesn't seem to support this operation.

I have the following loop in my code which spawns multiple threads on the ThreadPool. When I click a button on my UI, I would like these threads (or asynchronous calls) to end.

foreach (var sku in skus)
{
    loadSku.BeginInvoke(...
}

Is there any elegant solution other than creating a global "Cancel flag" and having the asynchronous methods to look for it?

like image 618
Martin Avatar asked Nov 13 '09 13:11

Martin


People also ask

How do I stop async calls?

You can cancel an asynchronous operation after a period of time by using the CancellationTokenSource. CancelAfter method if you don't want to wait for the operation to finish.

How do I terminate async function?

The idea is to pass a CancellationToken to every async function, then wrap every promise in AsyncCheckpoint . So that when the token is cancelled, your async function will be cancelled in the next checkpoint.

How do you stop asynchronous call in node JS?

When a method is running, and another method is called to stop the first method, I start an infinite loop to stop that code from running and then remove the method from the queue(array).

Why did I get an async call?

The async caller id is usually created by the robocalls via the spoofed and the customer area code. The main reason for introducing this new tech is because some people make use of blocked features on Spectrum. The prefix that is added in it ensures it looks genuine, making people able to receive the robocall.


2 Answers

A "cancel flag" is the way to do it, though not a global one, necessarily. The unavoidable point is that you need some way to signal to the thread that it should stop what it's doing.

In the case of BeginInvoke, this is hard to do with anything but a global flag, because the work is carried out on the threadpool, and you don't know which thread. You have a couple of options (in order of preference):

  1. Use the BackgroundWorker instead of BeginInvoke. This has cancellation functionality baked-in. This has other benefits, like progress monitoring, and "Work complete" callbacks. It also nicely handles exceptions.
  2. Use ThreadPool.QueueUserWorkItem, passing in an object as the state that has a Cancel() method that sets a Cancelled flag that the executing code can check. Of course you'll need to keep a reference to the state object so you can call Cancel() on it (which is something the BackgroundWorker does for you - you have a component on your form. (Thanks to Fredrik for reminding about this).
  3. Create your own ThreadStart delegate, passing in a state object as with option 2.
like image 113
Neil Barnwell Avatar answered Oct 11 '22 16:10

Neil Barnwell


If you're lookin for a "TerminateAsnyc" method, you won't find one. Therefore, no, there's probably no elegant way while using Control.BeginInvoke/EndInvoke. Thus, I'd put the boolean flag on the UI thread and have the delegate being executed asynchronously check that flag periodically while it's executing.

However, you might check into using background worker threads.

like image 41
Travis Heseman Avatar answered Oct 11 '22 16:10

Travis Heseman