Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You mentioned in one of the comments from my other answer a link, it looks like that link describes how to make it cancellable. I have adapted my example to support this:</p> <pre><code> private Dictionary&lt;Guid, TaskTracker&gt; _taskMap = new Dictionary&lt;Guid, TaskTracker&gt;(); private void OnButton1Click(object sender, EventArgs eventArgs) { TaskTracker taskTracker = new TaskTracker(Guid.NewGuid(), OnDoWork); _taskMap.Add(taskTracker.Identity, taskTracker); taskTracker.Start(); } private void OnDoWork(CancellationToken token) { for (int i = 0; i &lt; 100; i++) { Thread.Sleep(100); // Do some work, checking if cancel requested every once in a while if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } } } private void OnButton2Click(object sender, EventArgs eventArgs) { Guid identity = _taskMap.Count &gt; 0 ? _taskMap.First().Value.Identity : default(Guid); // find some way to get the desired task TaskTracker taskTracker; if (_taskMap.TryGetValue(identity, out taskTracker)) { taskTracker.TaskExiting += OnTaskExiting; taskTracker.Stop(); } } private void OnTaskExiting(object sender, EventArgs eventArgs) { TaskTracker taskTracker = (TaskTracker)sender; taskTracker.TaskExiting -= OnTaskExiting; _taskMap.Remove(taskTracker.Identity); Console.WriteLine("Time ellapsed for No partitioning at all version {0}ms , thread id was {1}", taskTracker.Stopwatch.ElapsedMilliseconds, Task.CurrentId); } private void OnButton3Click(object sender, EventArgs eventArgs) { Guid identity = _taskMap.Count &gt; 0 ? _taskMap.First().Value.Identity : default(Guid); // find some way to get the desired task TaskTracker taskTracker; if (_taskMap.TryGetValue(identity, out taskTracker)) { taskTracker.TaskExiting += OnTaskExiting; taskTracker.Cancel(); } } </code></pre> <p>And here is the TaskTracker class:</p> <pre><code>public class TaskTracker { private readonly Action&lt;CancellationToken&gt; OnDoWork; private readonly CancellationTokenSource TokenSource; private readonly CancellationToken Token; private bool _isRunning; private Task _task; public TaskTracker(Guid identity, Action&lt;CancellationToken&gt; onDoWork) { TokenSource = new CancellationTokenSource(); Token = TokenSource.Token; Identity = identity; OnDoWork = onDoWork; } public readonly Guid Identity; public readonly Stopwatch Stopwatch = new Stopwatch(); public event EventHandler TaskExiting; public void Start() { _isRunning = true; _task = Task.Factory.StartNew( () =&gt; { Stopwatch.Start(); try { while (_isRunning) { OnDoWork(Token); } } finally { Stopwatch.Stop(); if (TaskExiting != null) { TaskExiting(this, EventArgs.Empty); } } }, Token ); } public void Stop(bool waitForTaskToExit = false) { if (_task == null) { throw new InvalidOperationException("Task hasn't been started yet"); } _isRunning = false; if (waitForTaskToExit) { _task.Wait(); } _task = null; } public void Cancel() { if (_task == null) { throw new InvalidOperationException("Task hasn't been started yet"); } _isRunning = false; TokenSource.Cancel(); _task = null; } } </code></pre> <p>Basically, in your "Worker" you would check to see if the token indicates it is being cancelled, if so do some cleanup and then call the method ThrowIfCancellationRequested on token to break out.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload