forked from altmp/coreclr-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TickSynchronizationContext.cs
42 lines (35 loc) · 1.07 KB
/
TickSynchronizationContext.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace AltV.Net.Async
{
class TickSynchronizationContext : SynchronizationContext
{
private static readonly ConcurrentQueue<Action> Tasks = new ConcurrentQueue<Action>();
public override void Post(SendOrPostCallback d, object state)
{
Tasks.Enqueue(() => d(state));
}
private static int _runs;
private static Action _currentTask;
public static void Tick()
{
_runs = Tasks.Count;
while (_runs-- > 0 && Tasks.TryDequeue(out _currentTask))
{
try
{
_currentTask();
}
catch (Exception e)
{
Console.WriteLine($"Exception during executing of tick in TickSynchronizationContext: {e}");
}
}
}
public override SynchronizationContext CreateCopy()
{
return this;
}
}
}