I wanted a clean way to spin up a looping task in C# and do things while the task is running.

Conceptually, I want

  1. Start the “service”
  2. Run procedure
  3. Stop the “service”

This would be simple if the “service” had a start / stop button. However, the “service” runs like dead man’s switch: unless it’s pinged every now and then, it dies. When it’s first pinged, it starts up. Specifically, what I had was:

  • A “service” that dies if I don’t ping it every 10 seconds
  • A procedure that need to be run against the “service” while it’s alive

I needed a clean way to write my routine without resorting to Thread.Sleep and for loop hell. The obvious answer’s threading – put the timer and ping on a background thread, run the procedure, then kill the thread. This is probably simple using the Task Parallel Library in C#. However, I did not want to do so imperatively and start / stop the thread explicitly; doing so results in uglier / less readable code.

Turns out IDisposable gives me a clean way to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Threading;
using System.Threading.Tasks;

namespace DisposableThread
{
internal class Program
{
public static void Main(string[] args)
{
using (new DisposableThread(() => Console.WriteLine("DisposableThread running"), 100))
{
for (var i = 0; i < 4; i++)
{
Console.WriteLine("Main running");
Thread.Sleep(350);
}
}
}
}

public class DisposableThread : IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly Task _task;

public DisposableThread(Action action, int delayMilliseconds)
{
Console.WriteLine("DisposableThread started");

_cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;

_task = Task.Run(async () =>
{
while (!token.IsCancellationRequested)
{
action();
await Task.Delay(delayMilliseconds, token);
}
}, token);
}


public void Dispose()
{
_cancellationTokenSource.Cancel();
try
{
_task.Wait();
}
catch (AggregateException)
{
Console.WriteLine("DisposableThread task canceled");
}
Console.WriteLine("DisposableThread disposed");
}
}
}

This produces the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DisposableThread started
Main running
DisposableThread running
DisposableThread running
DisposableThread running
DisposableThread running
Main running
DisposableThread running
DisposableThread running
DisposableThread running
DisposableThread running
Main running
DisposableThread running
DisposableThread running
DisposableThread running
Main running
DisposableThread running
DisposableThread running
DisposableThread running
DisposableThread running
DisposableThread task canceled
DisposableThread disposed

Essentially, I wrapped the action I needed to do (in this case, a simple print to console () => Console.WriteLine("DisposableThread running") in a task:

1
2
3
4
5
6
7
8
async () =>
{
while (!token.IsCancellationRequested)
{
action();
await Task.Delay(delayMilliseconds, token);
}
}

If you extract this lambda into its own method, it would return a Task encapsulating encapsulating the multiple possible awaits. The lambda holds on to the cancellation token token that allows us to act on cancellation requests (which can then be issued by the Dispose() method of IDisposable).

Additionally, it’s important that we use Task.Delay(delayMilliseconds, token) instead of Thread.Sleep(delayMilliseconds). This allows us to react to cancellations during the delay instead of after the delay.

Finally, the dispose method needs to call Task’s Wait():

1
2
3
4
5
6
7
8
9
10
11
12
13
public void Dispose()
{
_cancellationTokenSource.Cancel();
try
{
_task.Wait();
}
catch (AggregateException)
{
Console.WriteLine("DisposableThread task canceled");
}
Console.WriteLine("DisposableThread disposed");
}

When we call _cancellationTokenSource.Cancel(), we are cancelling the task(s) that hold on to the token. Each of the tasks then throws a TaskCanceledException. This exception is not thrown in the main thread until it’s either handled explicitly or the garbage collector collects and finalizes the object. That means if the TaskCanceledException isn’t handled explicitly and the main thread goes on long enough, we may encounter an exception out of nowhere. Hence, we force the exception (which, like all Task exceptions, gets rolled into an AggregateException) by calling _task.Wait() and catch the exception explicitly.

This allows me to write better looking code like this:

1
2
3
4
5
using (SessionFactory.PingedSession())
{
// interact with session
// interact more with session
}

without worrying about starting and stopping the background pinging thread.