WPF中使用:
Task.Run(() =>
{
Console.WriteLine("222222-{0}", Thread.CurrentThread.ManagedThreadId);
Application.Current.Dispatcher.Invoke(new Action(() =>
{
Console.WriteLine("333333-{0}", Thread.CurrentThread.ManagedThreadId);
}));
Console.WriteLine("444444-{0}", Thread.CurrentThread.ManagedThreadId);
});
Task.Run(() =>
{
Console.WriteLine("222222-{0}", Thread.CurrentThread.ManagedThreadId);
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
Console.WriteLine("333333-{0}", Thread.CurrentThread.ManagedThreadId);
}));
Console.WriteLine("444444-{0}", Thread.CurrentThread.ManagedThreadId);
});
WinForm中使用:
Task.Run(() =>
{
Console.WriteLine("222222-{0}", Thread.CurrentThread.ManagedThreadId);
Invoke(new Action(() =>
{
Console.WriteLine("333333-{0}", Thread.CurrentThread.ManagedThreadId);
}));
Console.WriteLine("444444-{0}", Thread.CurrentThread.ManagedThreadId);
});
Task.Run(() =>
{
Console.WriteLine("222222-{0}", Thread.CurrentThread.ManagedThreadId);
BeginInvoke(new Action(() =>
{
Console.WriteLine("333333-{0}", Thread.CurrentThread.ManagedThreadId);
}));
Console.WriteLine("444444-{0}", Thread.CurrentThread.ManagedThreadId);
});