2012-12-07 70 views
3

我想用一個分派器創建一個線程,然後從另一個線程使用該分派器對具有不同優先級的工作進行排隊。即設置一個分派器到優先級隊列工作

var dispatcher = GetNewThreadDispatcher(); 
dispatcher.BeginInvoke(longRunningTask1, DispatcherPriority.Normal); 
dispatcher.BeginInvoke(longRunningTask2, DispatcherPriority.Background); 
dispatcher.BeginInvoke(longRunningTask3, DispatcherPriority.Normal); 

在這種情況下,後臺線程將執行longRunningTask1,然後longRunningTask2longRunningTask3。我無法創建一個乾淨的GetNewThreadDispatcher(),有什麼幫助嗎?

+0

是否有任何理由不想使用應用程序分派器? –

+0

新線程用於後臺工作,與UI或渲染無關,只是使用Dispatcher對象來管理線程的工作。 –

+0

BeginInvoke將啓動一個新的線程: 'Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,(Action)delegate {MyMethod();});' –

回答

2

你可以嘗試創建新的後臺線程(http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx),並得到Dispatcher它:

Dispatcher dispatcher = Dispatcher.FromThread(workerThread); 

部分編輯: 您需要執行內部OnThreadStart
Dispatcher dispatcher=Dispatcher.CurrentDispatcherDispatcher.Run。但有趣的事:後執行

workerThread.Start(); 
Dispatcher dispatcher = Dispatcher.FromThread(workerThread); 

dispatchernull但執行

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應用程序工作但我不是多線程專家。可能有人會糾正我或在此回覆中添加一些信息。

+0

我的理解是,只有當新線程已經有一個調度程序時,它纔會起作用,否則它將返回null。 –

+0

加入回答 – Frank59

+0

GetNewThreadDispatcher()的主體是什麼樣的? –