How to cancel an asynchronous task via web request?

Here is the scenario: When a user logs in, I run a task that listens for IMAP in standby mode for mail notifications and has a live connection to the client via signalr (a type of push notification). Now the problem is, how do I cancel this task? that is, the user cancels the push notification or logs out.

EDIT: according to my understanding, for example, if 5 users have logged in to the site, are there 5 running tasks? since I can cancel individual tasks.

+7
source share
3 answers

Asynchronous worker must be modified to support undo

http://www.csharp-examples.net/cancel-asynchronous-method/

You can also use a background worker, which is a bit less complicated http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx

The problem that I encountered when you send to cancel an external source (DB, app) that has already started the task, if it does not support cancellation, you may need to wait for completion.

EDIT: I never did this with MVC, but found a good article in the article “Using the cancellation token” for the asycn process that accepts the CancellationToken parameter in MVC4. http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4 hope this helps.

+1
source

Whenever you create a task, you must create a cancellation token and pass it. Since each user is associated with a task, you can store the task and cancellation token in a user session.

Now, using the cancellation token stored in the user's session, you can easily cancel the task when he logs out or presses the cancel button.

See post for canceling a task.

+1
source

A process can kill itself without user activity. The easiest way to exchange variables in another thread is with a file. Suppose the client browser is still open, the javascript timer raises Ajax requests, which writes the timestamp to the server to the file. Your task also periodically reads the file, and when the timestamp is too old, the process kills itself. The activity timeout value that you can select of your choice.

0
source

All Articles