Skip to content

Instantly share code, notes, and snippets.

View i3arnon's full-sized avatar

Bar Arnon i3arnon

View GitHub Profile
@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("ObjectDisposed_Generic"));
// prevent ThreadAbort while updating state
internal bool Change(uint dueTime, uint period)
{
bool success;
lock (TimerQueue.Instance) // Global lock.
{
if (m_canceled)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
// prevent ThreadAbort while updating state
@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)
{
@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);
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 / 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
@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));
}
});
static void Main()
{
OuterOperationAsync().Wait();
}
static async Task OuterOperationAsync()
{
Console.WriteLine(LogicalFlow.CurrentOperationId);
using (LogicalFlow.StartScope())
{
@i3arnon
i3arnon / 01-LogicalOperationStack-02.cs
Created October 6, 2015 21:21
LogicalFlow - A wrapper over LogicalOperationStack
public static class LogicalFlow
{
public static Guid CurrentOperationId
{
get
{
return Trace.CorrelationManager.LogicalOperationStack.Count > 0
? (Guid) Trace.CorrelationManager.LogicalOperationStack.Peek()
: Guid.Empty;
}
@i3arnon
i3arnon / 01-LogicalOperationStack-03.txt
Created October 6, 2015 21:22
Output using LogicalOperationStack
00000000-0000-0000-0000-000000000000
49985135-1e39-404c-834a-9f12026d9b65
54674452-e1c5-4b1b-91ed-6bd6ea725b98
c6ec00fd-bff8-4bde-bf70-e073b6714ae5
54674452-e1c5-4b1b-91ed-6bd6ea725b98