2011-10-19 62 views
1

我有一個WinCE C#應用程序輪詢(JAVA)服務器異步進入的消息。我想要實現的是我想要輪詢服務器,並在返回結果時將其排隊,然後將結果處理到另一個線程中。我想從進程線程中分離出異步接收線程,因爲在處理完響應之後,我可能必須對服務器進行額外的POST。異步結果在另一個線程

我有什麼,現在就在這裏

http://msdn.microsoft.com/en-us/library/86wf6409%28v=vs.80%29.aspx

出用於製造異步請求到服務器和讀取響應類似於一個類。我已經修改了該類以包含自定義超時,但這應該不重要。

在我的應用程序中會發生什麼,我開始在線程'pollingThread'中進行輪詢,並從服務器獲取響應。如果超時,響應將爲空,否則我嘗試處理響應。

我想將響應字符串發送給另一個可以處理它的線程,而我的pollingThread返回以繼續輪詢服務器。我無法弄清楚如何獲得響應到另一個線程。我知道如何在大窗口上使用Monitor.Pulse,但不幸的是,它在.NETCF 3.5上不可用。

任何見解?

感謝

編輯:@ Damon8or

我嘗試使用自動復位,但由於某種原因,WaitOne的()並沒有意識到另一個線程已經設置()事件,並因此錯過了數據通過。下面是我有:

的的AutoResetEvent _process聲明爲static和可見這兩種方法

ThreadStart processMethod= new ThreadStart(process); 
processingThread= new Thread(processJSONMethod); 
processingThread.IsBackground = true; 
processingThread.Start(); 

ThreadStart startMethod = new ThreadStart(Poll); 
connectionThread = new Thread(startMethod); 
connectionThread.IsBackground = true;      
connectionThread.Start(); 

的民意調查裏面,我有_process.Set()從服務器接收一個字符串之後。在處理方法中,我有:

while (_keepPolling) 
{ 

_process.WaitOne(); 

string text= MyQueue.Dequeue(); 
Debug.WriteLine("Returned: " + text 

}  

而且我看不到打印的調試行。輪詢方法產生並排隊字符串並返回到輪詢。

+0

你的問題到底是什麼? –

+0

我想確保在結果正在另一個線程中處理的同時在我的pollingThread中保持輪詢。我無法弄清楚如何在結果被推送到隊列後喚醒處理結果的線程 – Rishi

回答

1

您可以使用的AutoResetEvent或ManualResetEvent的信號您的工作線程。這是一個簡單的例子。

using System; 
    using System.Linq; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.Threading; 

    namespace QueueDoodles 
    { 
    class Program 
    { 
     public static readonly string[] data = { "a", "b", "c", "d", "e", "f", "g", "h" }; 

     static void Main(string[] args) 
     { 
      var running = true; 
      var rand = new Random(); 
      var q = new Queue<string>(); 
      var qEvent = new ManualResetEvent(false); 
      var pollThread = new Thread(new ThreadStart(delegate() 
      { 
       while (running) 
       { 
        // Queue the next value 
        var value = data[rand.Next(data.Length)]; 
        q.Enqueue(value); 
        Console.WriteLine("{0} queued {1}", Thread.CurrentThread.Name, value); 

        // Signal waiting thread 
        qEvent.Set(); 

        // Simulate polling 
        Thread.Sleep(rand.Next(100)); 
       } 
      })); 
      pollThread.Name = "Poll Thread"; 

      var workerThread = new Thread(new ThreadStart(delegate() 
      { 
       while (running) 
       { 
        // Wait on the queue 
        if (!qEvent.WaitOne()) 
         break; 
        qEvent.Reset(); 

        // Process the next queue item 
        var value = q.Dequeue(); 
        Console.WriteLine("{0} queued {1}", Thread.CurrentThread.Name, value); 
       } 
      })); 
      workerThread.Name = "Worker Thread"; 

      // Start the poll thread 
      pollThread.Start(); 

      // Give it some time to fill queue 
      Thread.Sleep(1000); 

      // Start the worker thread 
      workerThread.Start(); 

      // Wait for keyboard input 
      Console.ReadLine(); 
      running = false; 
      qEvent.Set(); 
     } 
    } 
} 
+0

請看這個問題的編輯,我無法將它全部納入評論。謝謝 – Rishi

+0

我加了Thread.Sleep(10);剛好在集合之後,它似乎幫助了WaitOne()擺脫了等待。不知道該怎麼做,但現在我有另一個問題,其中兩個線程不是並行操作。處理線程似乎阻止了connectionThread,即使它不應該。 就像一個健全的檢查,我有兩個線程作爲後臺線程。這有什麼區別嗎?謝謝 – Rishi

+0

我嘗試在我的QueueDoodle代碼中切換到AutoResetEvent,並且它工作得很好 - 在Reset()調用中註釋掉了,因爲這是使用該類型自動完成的。事件或處理線程必須有一些事情發生。我已經廣泛地使用這些事件類,並且它們始終工作。有些事情要嘗試... - 在處理線程中添加調試狀態以查看它是否正在運行。 - 爲WaitOne(1000)添加超時,並檢查返回以查看事件是否正在觸發。 - 嘗試ManualResetEvent以查看它的行爲是否有所不同。 – Damon8or

相關問題