-
Notifications
You must be signed in to change notification settings - Fork 8
/
Defer.cs
24 lines (22 loc) · 1.01 KB
/
Defer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace ThreadAffinity;
public class Defer(Action callback)
{
private readonly ExecutionContext? _context = ExecutionContext.Capture()!;
public void Run()
{
if (_context is null) { callback(); return; }
// When ExecutionContext.Run invokes the lambda we supply as the 2nd
// argument, it passes that lambda the value we supplied as the 3rd
// argument to Run. Here we're passing callback, so the lambda has
// access to the Action we want to invoke. It would have been simpler
// to write "_ => callback()", but the lambda would then need to
// capture 'this' to be able to access callback, and that capture
// would cause an additional allocation. Using the static keyword
// on the lambda tells the compiler that we intend to avoid capture,
// so it would report an error if we accidentally used any locals.
ExecutionContext.Run(
_context,
static (cb) => ((Action)cb!)(),
callback);
}
}