Bu tam olarak kolay değil, ama bu gerçekten de zor değil. Yapmanız gereken şey, STA olarak ayarlanan bir çalışan iş parçacığı döndürmek ve üzerinde Dispatcher çalışma zamanını başlatmanızdır. Orada oturan bir işçiye sahip olduğunuz zaman, bu tür bir iş için başlangıçta başlatılmayan ünite test ipliklerinden iş yükleyebilirsiniz. Bu yüzden, önce burada test kurulumunda memuru iplik başlangıç yapabilirsiniz:
this.dispatcherThread = new Thread(() =>
{
// This is here just to force the dispatcher infrastructure to be setup on this thread
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
{
Trace.WriteLine("Dispatcher worker thread started.");
}));
// Run the dispatcher so it starts processing the message loop
Dispatcher.Run();
});
this.dispatcherThread.SetApartmentState(ApartmentState.STA);
this.dispatcherThread.IsBackground = true;
this.dispatcherThread.Start();
Şimdi, temiz Yaptığın, sadece yapmak tavsiye Test temizleme, aşağıdaki şu konu kapatmaya istiyorsanız :
Dispatcher.FromThread(this.dispatcherThread).InvokeShutdown();
Yani, yoldan tüm bu altyapı şeyler, burada söz konusu iş parçacığı üzerinde yürütmek için test yapmanız gereken bu.
public void MyTestMethod
{
// Kick the test off on the dispatcher worker thread synchronously which will block until the work is competed
Dispatcher.FromThread(this.dispatcherThread).Invoke(new Action(() =>
{
// FromCurrentSynchronizationContext will now resolve to the dispatcher thread here
}));
}
Çalıştı, çok teşekkür ederim! – Alberto