我正在理解如何正確使用BeginInvoke。我在一個控制檯應用程序中寫了一個小測試,我所要做的就是使用BeginInvoke調用一個函數來創建一個帶有標題的100x100窗口。我失敗了。這裏是我所擁有的,我知道這可能只是對線程的不理解(不是我強壯的套裝),但是我被困住了,沒有窗口彈出來,我只是在Main的readline處等待按鍵。執行從ThreadUITest開始。Dispatcher.Begin調用正確的用法?
static void ThreadUITest()
{
ThreadStart starter = new ThreadStart(threadFunc1);
Thread test = new Thread(starter);
test.IsBackground = true;
test.SetApartmentState(ApartmentState.STA);
test.Start();
}
static void threadFunc1()
{
dispatcher = Dispatcher.CurrentDispatcher; //Statically declared earlier
ThreadStart starter = new ThreadStart(threadFunc2);
Thread test = new Thread(starter);
test.IsBackground = true;
test.Start();
}
static void threadFunc2()
{
Action method = Draw;
Console.WriteLine("I'm here!");
//dispatcher.BeginInvoke((Action)(() => {Draw();}),DispatcherPriority.Render, null);
dispatcher.BeginInvoke(method, DispatcherPriority.Send, null);
}
static void Draw()
{
Window win = new Window();
win.Height = 100;
win.Width = 100;
win.Title = "A Window!";
win.Show();
}
感謝您的任何幫助。
當你使用'dispatcher.Invoke'時它工作嗎? –
不,它沒有。 –