title: .NET Core equivalent to Thread.Abort
body: <h2>Background</h2>
  <p>I have a <code>Service</code> abstraction. Each service has it own <code>WorkItem</code>. WorkItem able to start with some data. The service is limiting the excution time of <code>WorkItem</code>. Let's say that a single workitem can takes up to 60 seconds. After this, the <code>Service</code> should kill it.</p>
  <p>This code <strong>migrated from the .NET Framework</strong>, I created a <code>Thread</code> object which run the <code>Start(model)</code> method. Then the code was something like:</p>
  <pre><code>Thread t = new Thread(workItem.Start, model);
  t.start();
  if (!t.Join(TimeSpan.FromSeconds(60)))
      t.Abort();
  </code></pre>
  <p>The <code>Thread.Abort</code> was injecting an exception for the running thread, which lead it for immediately stop.</p>
  <p>Now, <strong>I moved the code to dotnet core</strong> - as you may know, when you calling <code>Thread.Abort()</code> your getting the following message:</p>
  <pre><code>System.PlatformNotSupportedException: Thread abort is not supported on this platform.
     at System.Threading.Thread.Abort()
     at ...
  </code></pre>
  <h2>The Goal</h2>
  <p>I want to limit the execution time of the <code>WorkItem</code> to specific amount of time. Note that this limitation should work also if you running code line like this:</p>
  <pre><code>Thread.Sleep(61000); // 61 seconds. should be stop after 60 seconds.
  </code></pre>
  <h2>Progress</h2>
  <p>On the dotnet core world, it's seems like it's going to the <code>Task</code> related solution. So, I thought to use <code>CancellationToken</code>. But its seems like its impossible to watch the &quot;Canceled&quot; event and stop immediately. The examples I saw are using <code>while (!canceled)</code> loops, which cant stop long operations (like <code>Thread.Sleep(1000000)</code>.</p>
  <h2>Question</h2>
  <p>How to do it right?</p>
  <h2>Update</h2>
  <p>I written this sample code:</p>
  <pre><code>public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock)
  {
      try
      {
          Task task = Task.Factory.StartNew(() =&gt; codeBlock());
          if (!task.Wait(timeSpan))
          {
              // ABORT HERE!
              Console.WriteLine(&quot;Time exceeded. Aborted!&quot;);
          }
          return task.IsCompleted;
      }
      catch (AggregateException ae)
      {
          throw ae.InnerExceptions[0];
      }
  }
  </code></pre>
  <p>And this <code>Main</code> file:</p>
  <pre><code>public static void Main(string[] args)
  {
      bool Completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(2000), () =&gt;
      {
          Console.WriteLine(&quot;start&quot;);
          Thread.Sleep(3000);
          Console.WriteLine(&quot;end&quot;);
      });
  
      Console.WriteLine($&quot;Completed={Completed}&quot;);
      Console.ReadLine();
  }
  </code></pre>
  <p>Expected: &quot;end&quot; wont be printed to the screen. Actual: &quot;end&quot; printed. Is there any alternative that can kill a <code>Task</code>?</p>