SynchronizationContext的研究之一(非WPF及Forms)

时间:2021-12-31 20:55:32
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace SynchronizationTest003
{
class Program
{
static void Main(string[] args)
{
SynchronizationContext sc = new SynchronizationContext();
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
testA(sc); Console.ReadKey();
} public static void testA(object state)
{
SynchronizationContext sc = state as SynchronizationContext;
Console.WriteLine("Start");
Console.WriteLine("Func A's ManagedThreadID is: " + Thread.CurrentThread.ManagedThreadId);
sc.Post(testB,state);
Console.WriteLine("End");
} public static void testB(object state)
{
Console.WriteLine("Func B's ManagedThreadID is: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
SynchronizationContext sc = state as SynchronizationContext;
sc.Post(testC,null);
} public static void testC(object state)
{
Console.WriteLine("Func C's ManagedThreadID is: " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
SynchronizationContext sc = state as SynchronizationContext;
}
}
}

运行结果:

SynchronizationContext的研究之一(非WPF及Forms)