I would like to cancel a task using a cancellation token as described here: http://msdn.microsoft.com/en-us/library/dd997396.aspx.

The only difference is that:

I would like to start a task when I browse to a specific controller/Action, and cancel the task(tokenSource.Cancel()) to be called when I LEAVE, browse away from that specific action (page) I'm currently on. How would i do it? Thanks

有帮助吗?

解决方案

I figured it out.

I wrote a custom Attribute Filter that does this. Then in the code I have the following:

tokenSource = new CancellationTokenSource();
CancellationToken ct = tokenSource.Token;

var taskRepository = UnityContainerSetup.Container.Resolve<ITaskRepository>();
taskRepository.GetTasksStatusAsync(siteId, tasksItem, ct); // ct is my cancellation token that I set to true => to cancel
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top