在我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);
}
}
雜項:其他人可以張貼在那裏我可能出了問題,而不是僅僅反對投票後,並沒有放棄的理由
你可能更喜歡使用像Rx或至少,如你已經提到的阻塞集合。 –
@NumLock我一直在不懈地尋找,但是我找不到任何與Rx相關的例子。你能爲我指出正確的方向嗎?我從來沒有用過它。 –