CodeSOD: Strongly Unrecommended

This post was originally published on this site

The Daily WTF

Asynchronous programming is hard. Because it’s so difficult, developers are constantly trying to find ways to make it simpler, whether it’s promises or callbacks, or the async/await pattern. It gets more difficult when you need to deal with handling exceptions- when a task fails, trying to recover from that failure in a separate thread is an extra special challenge.

Which brings us to Betty’s co-worker. Using C#’s Task objects, which tie into the async/await pattern, they wanted to simply ignore any exceptions thrown by one of those tasks. That’s your first WTF, of course. Their approach, however, is a larger one:

public static void Forget(this Task task) { task.ContinueWith(task2 => { try { task.Wait(); } #pragma warning disable 168 catch (Exception ex) #pragma warning restore 168 { // ignored } }).ConfigureAwait(false); }

First, let’s talk about ContinueWith. ContinueWith is meant as a continuation for a task. By continuation,

To read the full article click on the 'post' link at the top.