简体   繁体   English

每隔x秒同时启动多个功能?

[英]Start several functions at the same time in every x seconds?

I know how to start a function in x seconds, its something like this : 我知道如何开始在x秒后,它就像一个函数这样

private Timer timer1; 
public void InitTimer()
{
    timer1 = new Timer();
    timer1.Tick += new EventHandler(timer1_Tick);
    timer1.Interval = 2000; // in miliseconds
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    function1();
    function2();
    function3();
}

The problem here: The functions cannot run at the same time. 问题在于:函数不能同时运行。 Function2 can only run if Function1 is done. Function2只能在Function1完成时运行。 But I want them running at the same time. 但是我希望它们能够在同一时间运行。 Thats why I could do this: 这就是我能做到这一点的原因:

private void timer1_Tick(object sender, EventArgs e)
{
   Parallel.Invoke(
     () => Function1(),
     () => Function2(),
     () => Function3()
   );
}

Is this the smartest way to do it in c#? 这是用c#做最聪明的方法吗? What I dont understand with Parallel.Invoke : what if my timer is set for 5 sec and after 5 sec function1,2 and 3 are not done but I call them all again. 我对Parallel.Invoke不了解:如果我的计时器设置为5秒,5秒后功能1,2和3没有完成,但我再次调用它们。 Do I start these functions in a new thread? 我是否在线程中启动这些功能? Are there after some calls x-threads running function1() (at the same time)? 是否有一些调用x-threads运行function1()(同时)? Wondering is that really healthy. 想知道真的很健康。

If somebody would like to get more information: function1 is just there to copy file x from folder a to b, function2 is only there to read all files from folder b and save the information and function3 is only there to check the connection and if there is a connection send the appropriate file to somebody. 如果有人想获得更多信息: function1只是将文件x从文件夹a复制到b,则function2只在那里读取文件夹b中的所有文件并保存信息,而function3仅用于检查连接,如果有是一个连接发送适当的文件给某人。

Any suggestions to the code? 对代码的任何建议? Thank you 谢谢

Parallel.Invoke runs all your functions in parallel. Parallel.Invoke并行运行所有函数。 Since function2 shouldn't run before function1, create a task that runs them in background but sequentially. 由于function2不应该在function1之前运行,因此创建一个在后台运行它们但按顺序运行的任务。

Task.Factory.StartNew(() =>
{
    function1();
    function2();
    function3();
});

what if my timer is set for 5 sec and after 5 sec function1,2 and 3 are not done 如果我的计时器设置为5秒,5秒后,功能1,2和3未完成,该怎么办?

you can use a bool to see whether they are completed or not. 您可以使用bool来查看它们是否已完成。

Use System.Threading.Timer to schedule the callback on a ThreadPool thread, so you don't need to create a new task. 使用System.Threading.TimerThreadPool线程上安排回调,因此您不需要创建新任务。 It also allows you to control the intervals to prevent the overlap of callbacks. 它还允许您控制间隔以防止回调重叠。

// fired after 5 secs, but just once.
_timer = new System.Threading.Timer(Callback, null, 5000, Timeout.Infinite);

// on the callback you must re-schedule the next callback time;
private void Callback(Object state) {
   Function1();
   Function2();

   // schedule the next callback time 
   _timer.Change(5000, Timeout.Infinite);
}

Have you looked at Tasks? 你看过任务吗?

You could do something like this: 你可以这样做:

Task[] tasks = new Task[3];
tasks[0] = Task.Factory.StartNew(() => Function1());
tasks[1] = Task.Factory.StartNew(() => Function2());
tasks[2] = Task.Factory.StartNew(() => Function3());
Task.WaitAll(tasks);

The above will allow you to run the functions in parallel, which is what you imply you want. 以上将允许您并行运行这些功能,这就是您所希望的。

If you don't want to block inside the timer1_Tick waiting for all the Tasks to complete, then don't use WaitAll....instead you could check if the Tasks have completed by checking IsCompleted on the Tasks when your timer gets fires again....then decide whether to issue the next batch of functions. 如果您不想在timer1_Tick等待所有任务完成,那么不要使用WaitAll ....而是可以通过在计时器再次触发时检查任务上的IsCompleted来检查任务是否已完成....然后决定是否发布下一批功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM