新しいバックグラウンド スレッド ( http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx ) を作成して取得Dispatcher
することができます。
Dispatcher dispatcher = Dispatcher.FromThread(workerThread);
いくつかの編集: を実行する
Dispatcher dispatcher=Dispatcher.CurrentDispatcher
か、Dispatcher.Run
内部で実行する必要がありOnThreadStart
ます。しかし興味深いこと: 実行後
workerThread.Start();
Dispatcher dispatcher = Dispatcher.FromThread(workerThread);
dispatcher
null
実行後ですが
workerThread.Start();
Console.WriteLine("main thread: Starting worker thread...");
Dispatcher dispatcher = Dispatcher.FromThread(workerThread);
dispatcher
いっぱいです
追加した:
static void OnThreadStart()
{
Dispatcher.Run();
}
private Dispatcher GetNewThreadDispatcher()
{
Thread workerThread=null;
try
{
workerThread = new Thread(OnThreadStart);
workerThread.IsBackground = true;
workerThread.Start();
int waitingCiclesCount = 100;
int cicleIndex = 0;
int sleepTimeInMiliseconds = 100;
Dispatcher dispatcher = null;
while (cicleIndex < waitingCiclesCount)
{
dispatcher = Dispatcher.FromThread(workerThread);
if (dispatcher!=null)
break;
Thread.Sleep(sleepTimeInMiliseconds);
cicleIndex = cicleIndex + 1;
}
if (dispatcher==null)
{
workerThread.Abort();
return null;
}
Console.WriteLine(String.Format("thread with id={0} started", workerThread.ManagedThreadId));
return dispatcher;
}
catch (Exception)
{
if (workerThread!=null)
workerThread.Abort();
return null;
}
}
public MainWindow()
{
InitializeComponent();
TestWorker worker=new TestWorker();
Dispatcher dispatcher1 = GetNewThreadDispatcher();
if(dispatcher1!=null)
dispatcher1.BeginInvoke(new TestDelegate(worker.DoWork1), DispatcherPriority.Normal);
else
{
MessageBox.Show("Cant create dispatcher1");
}
Dispatcher dispatcher2 = GetNewThreadDispatcher();
if (dispatcher2!=null)
dispatcher2.BeginInvoke(new TestDelegate(worker.DoWork2), DispatcherPriority.Normal);
else
{
MessageBox.Show("Cant create dispatcher2");
}
}
このコードは私のテスト wpf アプリで動作しますが、私はマルチスレッドの専門家ではありません。誰かが私を訂正するか、この返信に情報を追加するかもしれません.