2017-07-13 55 views
-1

在我CheckQueue()方法(通過調用AppQ.Connect(Time))和TryDequeue每個項目關閉它我想要做的就是不斷地從隊列中讀到的東西。經過一段時間(也許一分鐘左右)後,我想返回到隊列 - 再次連接,檢查它是否有一些元素(AppQ.Count),閱讀它們(通過TryDequeue()),並無限期地執行此過程。循環通過ConcurrentQueue <T>無限期地利用基於定時器/秒錶事件

這是一個應用程序,它將成爲一個Windows服務(目前是用於測試目的的Windows Forms應用程序),每天運行一整天,並在某些機器上運行的任何應用程序出現停機時警告我們開發人員。我知道我將不得不使用While-Loop,但我不確定是否使用基於Timer的事件或Stopwatch以及它。

更多上下文:我也有,我應該考慮使用do-while循環爲(通過調用AppQ.Connect(time)負荷消息)而不是一個while看到,因爲前者執行一些動作的感覺,然後檢查退出條件(如果AppQ.Count <= 0)而while則相反。

考慮到它是線程安全的,我應該使用BlockingCollection<T>嗎?

有什麼建議嗎?

public class APPInfo 
{ 
     public String Machine { get; set; } 
     public String IP { get; set; } 
     public String Port { get; set; } 
     public String App { get; set; } 
     public ClientApplicationState Status { get; set; } 
     public String Message { get; set; } 
     public String TimeStamp { get; set; } 
     public String CPUUse { get; set; } 
     public String MemoryUse { get; set; } 
     public String TotalCPUUse { get; set; } 
     public String TotalMemoryUse { get; set; } 
} 

public static ConcurrentQueue<APPInfo> AppQ = new ConcurrentQueue<APPInfo>(); 

public static void Connect(Int16 Time) 
{  
     AppQ.Enqueue(new APPInfo { Machine = "One", IP = "127.0.0.1", Port = "23000", App = "TestServer1", Status = ClientApplicationState.OK, TimeStamp = DateTime.Now.ToString(), CPUUse = "80.0", MemoryUse = "81.0", TotalCPUUse = "86.0", TotalMemoryUse = "87.0" }); 

     AppQ.Enqueue(new APPInfo { Machine = "One", IP = "127.0.0.1", Port = "23001", App = "TestServer2", Status = ClientApplicationState.ERROR, TimeStamp = DateTime.Now.ToString(), CPUUse = "82.0", MemoryUse = "83.0", TotalCPUUse = "88.0", TotalMemoryUse = "89.0" }); 

     AppQ.Enqueue(new APPInfo { Machine = "Two", IP = "127.0.0.2", Port = "23002", App = "TestServer3", Status = ClientApplicationState.WARNING, TimeStamp = DateTime.Now.ToString(), CPUUse = "84.0", MemoryUse = "85.0", TotalCPUUse = "90.0", TotalMemoryUse = "91.0" });   
} 

private void CheckQueue() 
{ 
    AppIP.Connect(50); 
    APPInfo first = new APPInfo();   

    if (AppIP.AppQ.Count > 0) 
    { 
     AppIP.AppQ.TryDequeue(out first); 

     MessageBox.Show(first.App, first.IP, first.Port, first.Status.ToString(), first.Message, first.TimeStamp, first.CPUUse, first.MemoryUse, first.Machine, first.TotalCPUUse, first.TotalMemoryUse + "\n"); 
    } 
    else 
    { 
      MessageBox.Show("There is nothing in the queue to process.", "Important Note", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); 
    } 
} 

雜項:其他人可以張貼在那裏我可能出了問題,而不是僅僅反對投票後,並沒有放棄的理由

+1

你可能更喜歡使用像Rx或至少,如你已經提到的阻塞集合。 –

+0

@NumLock我一直在不懈地尋找,但是我找不到任何與Rx相關的例子。你能爲我指出正確的方向嗎?我從來沒有用過它。 –

回答

1

爲了在一段時間檢查一次隊列,你需要設置意見一個計時器:

CheckTimer = new System.Threading.Timer(CheckQueue, null, TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(1000)); 

您應該在初始化服務時創建計時器。

在您的CheckQueue方法中,您將需要一個循環來處理隊列中的所有數據。

private void CheckQueue(object dummy) 
{ 
    // The dummy is null, its there because it is a Timer event. 
    AppIP.Connect(50); 

    AppInfo appInfo; 
    while(AppQ.TryDequeue(out appInfo)) 
    { 
     // Process the appInfo 
    } 
} 

注意,從的System.Threading定時器將在不同的線程運行,下一個事件的處理會發生,即使當前事件仍在運行!

+0

你在哪裏使用'CheckTimer'? –

+0

另外,參數化對象是做什麼的?你的回答很模糊嘿。請看看你是否可以更新它? @Casperah –

+0

我已經添加了一些評論來回答你的問題,但答案有什麼問題,爲什麼-1? – Casperah