C#如何在不停的同时运行多个任务

C#如何在不停的同时运行多个任务,c#,task,C#,Task,我有一个服务器,它在while(true)循环中侦听客户机。 我将每个客户机的主机名保存在一个列表中,并节省客户机与服务器联系的时间。 我想每10分钟检查一次,看看是否有一些客户端在过去的一小时内没有联系服务器,并打印它的名字。 我想做这样的事情: Task.Run(CheckTheClients()) //Check the passed-time of each client in the list while(true) { //listen to clients, add them to

我有一个服务器,它在while(true)循环中侦听客户机。 我将每个客户机的主机名保存在一个列表中,并节省客户机与服务器联系的时间。 我想每10分钟检查一次,看看是否有一些客户端在过去的一小时内没有联系服务器,并打印它的名字。 我想做这样的事情:

Task.Run(CheckTheClients()) //Check the passed-time of each client in the list
while(true)
{
//listen to clients, add them to list, etc.
}
// insert this into a long running function, and scope the timer variable correctly 
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 600000;
myTimer.Enabled = true;


 // Define what you want to happen when the Elapsed event occurs (happens on the interval you set).
 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
     //do some work here
 }
while (true)
{
   try
   {
        // do something
   }
   catch (Exception ex)
   {
        // save log 
   }

   Thread.Sleep(TimeSpan.FromMilliseconds(TimeSpan.FromMinutes(10).TotalMilliseconds));
}
但我不知道如何每10分钟检查一次,而不是每毫秒检查一次,也不知道我的想法是否正确。 那么最好的方法是什么呢?
此外,函数和while(true)都涉及客户机列表。这会产生一些问题吗?

最好使用Timer函数,基本上是您创建它,向它传递一个函数以在每次传递的时间内调用,并将时间设置为以毫秒为单位的等待时间。以10分钟为例,类似这样:

Task.Run(CheckTheClients()) //Check the passed-time of each client in the list
while(true)
{
//listen to clients, add them to list, etc.
}
// insert this into a long running function, and scope the timer variable correctly 
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 600000;
myTimer.Enabled = true;


 // Define what you want to happen when the Elapsed event occurs (happens on the interval you set).
 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
     //do some work here
 }
while (true)
{
   try
   {
        // do something
   }
   catch (Exception ex)
   {
        // save log 
   }

   Thread.Sleep(TimeSpan.FromMilliseconds(TimeSpan.FromMinutes(10).TotalMilliseconds));
}

您可以将线程置于睡眠状态,如下所示:

Task.Run(CheckTheClients()) //Check the passed-time of each client in the list
while(true)
{
//listen to clients, add them to list, etc.
}
// insert this into a long running function, and scope the timer variable correctly 
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 600000;
myTimer.Enabled = true;


 // Define what you want to happen when the Elapsed event occurs (happens on the interval you set).
 private static void OnTimedEvent(object source, ElapsedEventArgs e)
 {
     //do some work here
 }
while (true)
{
   try
   {
        // do something
   }
   catch (Exception ex)
   {
        // save log 
   }

   Thread.Sleep(TimeSpan.FromMilliseconds(TimeSpan.FromMinutes(10).TotalMilliseconds));
}

既然您在提供的示例代码中使用了
Task.Run
,为什么不使用
Task.Delay

Action CheckTheClients = () => Console.WriteLine("Checking clients...");
while (true)
{
    var task = Task.Delay(1000).ContinueWith(x => CheckTheClients());
    await task;
}

为了简单起见,不需要注册计时器及其事件。

你是说,像计时器一样@艾米,谢谢。共享客户端列表是否会导致一些问题?这完全取决于您如何处理它。如有必要,请使用互斥锁或并发集合。谢谢。那共享资源呢,客户端列表。不会引起任何麻烦吗?根据我的想象,客户机列表并不是真正的共享资源,因为服务器是维护列表的唯一工具。如果您有多个线程运行此功能,那么您需要相应地进行规划,通常使用并发集合()。如果你有另一个线程正在处理集合,那么你有一个计时器在后台使用它,你肯定应该使用并发线程来确保没有死锁。使用基于TPL的模式来代替计时器可能有助于解决并发问题我喜欢你的想法,我认为计时器更简单。谢谢