Note that there are some explanatory texts on larger screens.

plurals
  1. POSubscriber method vs event
    primarykey
    data
    text
    <p>I have noticed a new tendency in .NET 4.0, specifically in potentially multi-threaded scenarios, which is avoiding events, and providing subscriber methods instead.</p> <p>For example, <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" rel="nofollow">System.Threading.Tasks.Task</a> and <code>Task&lt;TResult&gt;</code> have <a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.continuewith.aspx" rel="nofollow">ContinueWith()</a> methods instead of a Completed or Finished event. Another example is <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.aspx" rel="nofollow">System.Threading.CancellationToken</a>: it has a <a href="http://msdn.microsoft.com/en-us/library/system.threading.cancellationtoken.register%28VS.100%29.aspx" rel="nofollow">Register()</a> method instead of a CancellationRequested event.</p> <p>While Task.ContinueWith() is logical because it allows for easy task chaining (would not be so elegant with events), and that it also allows <code>Task&lt;TResult&gt;</code> to inherit from <code>Task</code> (because this way, <code>Task&lt;TResult&gt;</code> can provide appropriate overloads, which would not be possible for an event: if you have an event EventHandler Finished in Task, all you can do is create <em>another</em> event, say, event <code>EventHandler&lt;TaskResultEventArgs&gt;</code> Finished in <code>Task&lt;TResult&gt;</code>, which is not very nice), but I cannot find the same explanation for CancellationToken.Register().</p> <p>So, what are the disadvantages of events in similar scenarios? Should I also follow this pattern? To clarify, which one of the below should I choose? When should I prefer one against the other?</p> <pre><code>public event EventHandler Finished; // or public IDisposable OnFinished(Action continuation) </code></pre> <p>Thank you very much!</p>
    singulars
    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.
 

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