title: Cancelling external event handler raised inside task
body: <p>Quick question, I'm running a task that's raising an event which some other object is subscribing to. Is there a way to cancel that object's event handler mid-execution? Attempted to do that using cancellation tokens, </p>
  
  <pre><code>                    public event Action&lt;Int&gt; OnUpdate;
  
                      CancellationTokenSource cts = new CancellationTokenSource();
                      Task t = Task.Factory.StartNew(state =&gt; {
                          try {
                              var token = (CancellationToken)state;
                              token.ThrowIfCancellationRequested();
                              OnUpdate(_int);
                          } catch (Exception e) {
                              Console.WriteLine("Update threw exception : {0}", e.Message);
                          }
                      }, cts.Token, cts.Token);
  
                      if (!t.Wait(8000, cts.Token)) {
                          cts.Cancel();
                          Console.WriteLine("Update task did not finish in time.");
                      } else {
                          Console.WriteLine("Task did finish in time.");
                      }
  </code></pre>
  
  <p>The idea here is I'm starting a new task, t, which raises an event, OnUpdate, which is caught and handled by an external object. I want the task to cancel if it's taking more than 8 seconds to handle the update which was the idea behind the cancellation token usage. However, even if I call cts.Cancel(), the external event handler still runs to completion. Is there anything I can do?</p>
  
  <p>Edit - for clarity as a commenter pointed out, I'm stuck with using Action here as this is a public-facing API I'm editing. I could potentially move to spawning new Threads instead of tasks and then calling Thread.Abort() but that seems rather brutish; I would prefer a much nicer solution.</p>