Skip to content

Instantly share code, notes, and snippets.

View i3arnon's full-sized avatar

Bar Arnon i3arnon

View GitHub Profile
static void Main()
{
OuterOperationAsync().Wait();
}
static async Task OuterOperationAsync()
{
Console.WriteLine(LogicalFlow.CurrentOperationId);
using (LogicalFlow.StartScope())
{
@i3arnon
i3arnon / 02-LongRunning-03.cs
Created October 6, 2015 21:16
Task.Run over Task.Factory.StartNew
Task task = Task.Run(async () =>
{
while (IsEnabled)
{
await FooAsync();
await Task.Delay(TimeSpan.FromSeconds(10));
}
});
@i3arnon
i3arnon / 02-LongRunning-02.cs
Created October 6, 2015 21:14
ThreadPoolTaskScheduler.QueueTask creates a new thread for TaskCreationOptions.LongRunning
protected internal override void QueueTask(Task task)
{
if ((task.Options & TaskCreationOptions.LongRunning) != 0)
{
// Run LongRunning tasks on their own dedicated thread.
Thread thread = new Thread(s_longRunningThreadWork);
thread.IsBackground = true; // Keep this thread from blocking process shutdown
thread.Start(task);
}
else
Task<Task> task = Task.Factory.StartNew(async () =>
{
while (IsEnabled)
{
await FooAsync();
await Task.Delay(TimeSpan.FromSeconds(10));
}
}, TaskCreationOptions.LongRunning);
Task actualTask = task.Unwrap();
@i3arnon
i3arnon / 03-TimerContention-04.cs
Created October 6, 2015 21:01
CollectiveTimer usage
var collectiveTimer = new CollectiveTimer<CancellationTokenSource>(
cts => cts.Cancel(),
TimeSpan.FromMilliseconds(200),
cancellationToken);
@i3arnon
i3arnon / 03-TimerContention-03.cs
Created October 6, 2015 20:59
CollectiveTimer reduces timer allocations.
public class CollectiveTimer<T>
{
private readonly ConcurrentQueue<QueueItem> _queue;
public CollectiveTimer(Action<T> action, TimeSpan timeout, CancellationToken cancellationToken)
{
_queue = new ConcurrentQueue<QueueItem>();
Task.Run(async () =>
{
while (!cancellationToken.IsCancellationRequested)
{
internal bool Change(uint dueTime, uint period)
{
bool success;
lock (TimerQueue.Instance) // Global lock.
{
if (m_canceled)
throw new ObjectDisposedException(null, Environment.GetResourceString(&quot;ObjectDisposed_Generic&quot;));
// prevent ThreadAbort while updating state
@i3arnon
i3arnon / 03-TimerContention-02.cs
Created October 6, 2015 20:53
Timer.Change takes a lock on a singleton instance.
internal bool Change(uint dueTime, uint period)
{
bool success;
lock (TimerQueue.Instance)
{
if (m_canceled)
throw new ObjectDisposedException(null, Environment.GetResourceString(&quot;ObjectDisposed_Generic&quot;));
// prevent ThreadAbort while updating state
@i3arnon
i3arnon / 03-TimerContention-02.cs
Created October 6, 2015 20:52
Timer.Change takes a lock on a singleton instance.
internal bool Change(uint dueTime, uint period)
{
bool success;
lock (TimerQueue.Instance)
{
if (m_canceled)
throw new ObjectDisposedException(null, Environment.GetResourceString(&quot;ObjectDisposed_Generic&quot;));
// prevent ThreadAbort while updating state
@i3arnon
i3arnon / 03-TimerContention-01.cs
Last active October 6, 2015 21:05
High contention when creating new timers
static void Main()
{
for (var i = 0; i < Environment.ProcessorCount; i++)
{
Task.Factory.StartNew(() =>
{
while (true)
{
new Timer(_ => { }, null, TimeSpan.FromMilliseconds(100), Timeout.InfiniteTimeSpan);
}