在ConcurrentQueue< T>中循环.无限期地使用基于Timer/Stopwatch的事件 [英] Cycle through ConcurrentQueue<T> indefinitely using either Timer/Stopwatch based event

查看:60
本文介绍了在ConcurrentQueue< T>中循环.无限期地使用基于Timer/Stopwatch的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 CheckQueue()方法中,我想做的是不断从队列中读取(通过调用 AppQ.Connect(Time))和TryDequeue 每个项目都关闭.经过一段时间(可能是一分钟左右)后,我想返回队列-再次连接,检查它是否包含某些元素( AppQ.Count ),读取它们(按TryDequeue())并无限期地执行此过程.

In my CheckQueue() method, what I want to do is constantly read from the queue (by calling the AppQ.Connect(Time)) and TryDequeue each item off of it. After some elapsed time (maybe a minute or so), I want to go back to the queue - connect again, check if it has some elements (AppQ.Count), read them (by TryDequeue()) and do this process indefinitely.

这是针对将成为Windows服务的应用程序(当前是用于测试目的的Windows窗体应用程序),该服务每天全天运行,并在某些计算机上运行的任何应用程序出现停机时提醒我们开发人员.我知道我必须使用 While-Loop ,但是我不确定是使用基于 Timer 的事件还是使用 Stopwatch .

This is for an application which will be made a Windows Service (currently a Windows Forms Application for testing purposes) that will run all day everyday and alert us the developers when there is some downtime in any application running on some machine. I know I will have to use a While-Loop but I am unsure whether to use a Timer based event or a Stopwatch along with it.

更多上下文:我也有一种感觉,我应该考虑利用 do-while 循环,而不是 while ,因为前者执行了某些操作(通过调用 AppQ.Connect(time)加载消息),然后检查退出条件(如果 AppQ.Count< = 0 ),而在 while 相反.

More context: I also have a feeling that I should consider making use of a do-while loop as opposed to a while seeing as the former performs some action (load messages by calling AppQ.Connect(time)) then check the exit condition (if AppQ.Count <= 0) whereas a while does the opposite.

考虑到它是线程安全的,我应该使用 BlockingCollection< T> 吗?

Should I use a BlockingCollection<T> considering it is thread-safe?

有什么建议吗?

 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);
     }
 }

杂项:人们可以对我可能出错的地方发表评论,而不是仅仅对某篇文章投反对票而不给出理由吗

miscellaneous: Can people post comments on where I may have gone wrong instead of just down voting a post and not giving reason

推荐答案

为了偶尔检查队列,您需要设置一个计时器:

In order to check queue once in a while you need to setup a timer:

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

初始化服务时应创建计时器.

You should create the timer when you are initializing your service.

在CheckQueue方法中,您将需要一个循环来处理队列中的所有数据.

In your CheckQueue method you will need a loop to handle all the data in the queue.

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中的计时器将在不同的线程中运行,即使当前事件仍在运行,也会处理下一个事件!

Note that the timer from System.Threading will run in different threads and the processing of the next event will happen even if the current event is still running!

这篇关于在ConcurrentQueue&lt; T&gt;中循环.无限期地使用基于Timer/Stopwatch的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆