Skip to content

Instantly share code, notes, and snippets.

View i3arnon's full-sized avatar

Bar Arnon i3arnon

View GitHub Profile
var root = Task.Delay(-1);
var continuation = root.ContinueWith(t => t.GetAwaiter().GetResult(),new CancellationToken(true));
Console.WriteLine(root.IsCanceled);
Console.WriteLine(continuation.IsCanceled);
@i3arnon
i3arnon / proof
Created October 10, 2014 18:03
async method - synchronous part
private static void Main()
{
Console.WriteLine("before start");
var task = InfiniteAsync();
Console.WriteLine("after start");
Task.WhenAll(task).Wait();
Console.WriteLine("after whenAll");
}
private static async Task InfiniteAsync()
private static void Main()
{
Task.WhenAll(A(), B()).Wait();
}
private static async Task A()
{
Console.WriteLine(DateTime.Now + "started sync A");
Thread.Sleep(10000);
@i3arnon
i3arnon / NotRealCode.cs
Last active August 29, 2015 14:15
LogicalOperationStack (purely hypothetical)
public static class MyStack
{
private static Stack CurrentContext
{
get
{
return Trace.CorrelationManager.LogicalOperationStack;
}
}
@i3arnon
i3arnon / Program.cs
Created February 20, 2015 01:26
Zigzag
internal class Program
{
private static void Main()
{
var head = new Node();
CreateTree(head, 4, 2);
head.Print(string.Empty, true);
Console.WriteLine("Zigzag:");
var zig = new Stack<Node>();
@i3arnon
i3arnon / yarin.cs
Last active August 29, 2015 14:20
async for yarin
rivate static void Main()
{
CountToFiftyAsync().Wait();
}
private static async Task CountToFiftyAsync()
{
var taskA = CountToAsync("a", 10);
var taskB = CountToAsync("b", 10);
var taskC = CountToAsync("c", 10);
@i3arnon
i3arnon / 04-protobuf-01.cs
Last active October 4, 2015 21:58
Protobuf-net loses DateTime.Kind when deserializing.
static void Main()
{
var dolly = new Sheep {DateOfBirth = new DateTime(1966, 07, 05, 11, 0, 0, DateTimeKind.Utc)};
Console.WriteLine(dolly.DateOfBirth.ToString("HH:mm:ss K")); // "11:00:00 Z" (Z means UTC)
dolly = Serializer.DeepClone(dolly); // Serialize and deserialize using protobuf-net
Console.WriteLine(dolly.DateOfBirth.ToString("HH:mm:ss K")); // "11:00:00" (no Z means unspecified)
Console.WriteLine(dolly.DateOfBirth.ToLocalTime().ToString("HH:mm:ss K")); // "01:00:00 -10:00" (Hawaii timezone)
Console.WriteLine(dolly.DateOfBirth.ToUniversalTime().ToString("HH:mm:ss K")); // "21:00:00 Z"
}
@i3arnon
i3arnon / 04-protobuf-02.cs
Created October 2, 2015 22:33
Replacing EpochOrigin fixes the issue.
typeof (BclHelpers).
GetField("EpochOrigin", BindingFlags.NonPublic | BindingFlags.Static).
SetValue(null, new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
var dolly = new Sheep {DateOfBirth = new DateTime(1966, 07, 05, 11, 0, 0, DateTimeKind.Utc)};
Console.WriteLine(dolly.DateOfBirth.ToString("HH:mm:ss K")); // "11:00:00 Z" (Z means UTC)
dolly = Serializer.DeepClone(dolly); // Serialize and deserialize using protobuf-net
Console.WriteLine(dolly.DateOfBirth.ToString("HH:mm:ss K")); // "11:00:00 Z"
@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);
}
@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