Created
September 3, 2012 13:38
-
-
Save svick/3609385 to your computer and use it in GitHub Desktop.
SO Task inlining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[3] Entering Test, stop = False. | |
[3] Entering Test, stop = True. | |
[3] Exiting Test by stopping. | |
[3] Exiting Test normally. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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