Skip to content

Instantly share code, notes, and snippets.

@yutopio
Created October 18, 2016 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yutopio/58ae5240b697797abd0b9bc749ccc9cd to your computer and use it in GitHub Desktop.
Save yutopio/58ae5240b697797abd0b9bc749ccc9cd to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
class Program
{
static readonly EventWaitHandle wh = new ManualResetEvent(false);
static volatile bool beforeSet = false;
static volatile bool afterSleep = false;
static volatile bool insideCatch = false;
static volatile bool insideFinally = false;
static volatile bool afterTry = false;
static volatile bool afterTrySleep = false;
static void Main(string[] args)
{
var t = new Thread(SomeTask);
t.Start();
// Wait for another thread to be ready.
wh.WaitOne();
// Abor the thread and wait for its exit.
t.Abort();
t.Join();
Console.WriteLine("{0}\t{1}", nameof(beforeSet), beforeSet);
Console.WriteLine("{0}\t{1}", nameof(afterSleep), afterSleep);
Console.WriteLine("{0}\t{1}", nameof(insideCatch), insideCatch);
Console.WriteLine("{0}\t{1}", nameof(insideFinally), insideFinally);
Console.WriteLine("{0}\t{1}", nameof(afterTry), afterTry);
Console.WriteLine("{0}\t{1}", nameof(afterTrySleep), afterTrySleep);
}
static void SomeTask()
{
//RuntimeHelpers.PrepareConstrainedRegions();
try
{
beforeSet = true;
wh.Set();
Thread.Sleep(1000);
afterSleep = true;
}
catch (ThreadAbortException)
{
// Aborted !
insideCatch = true;
}
finally
{
insideFinally = true;
}
afterTry = true;
Thread.Sleep(1);
afterTrySleep = true;
}
static void SomeTask2()
{
RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
beforeSet = true;
wh.Set();
Thread.Sleep(1000);
afterSleep = true;
}
afterTry = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment