Skip to content

Instantly share code, notes, and snippets.

@svick
Created September 3, 2012 13:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svick/3609385 to your computer and use it in GitHub Desktop.
Save svick/3609385 to your computer and use it in GitHub Desktop.
SO Task inlining
[3] Entering Test, stop = False.
[3] Entering Test, stop = True.
[3] Exiting Test by stopping.
[3] Exiting Test normally.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
Task.Factory.StartNew(() => new Program().Test()).Wait();
}
object TestLock = new object();
public void Test(bool stop = false)
{
Console.WriteLine("[{0}] Entering Test, stop = {1}.", Thread.CurrentThread.ManagedThreadId, stop);
Task t;
lock (this.TestLock)
{
if (stop)
{
Console.WriteLine("[{0}] Exiting Test by stopping.", Thread.CurrentThread.ManagedThreadId);
return;
}
t = Task.Factory.StartNew(() => { this.Test(stop: true); });
}
t.Wait();
Console.WriteLine("[{0}] Exiting Test normally.", Thread.CurrentThread.ManagedThreadId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment