延迟启动流程 [英] start processes with a delay

查看:52
本文介绍了延迟启动流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何启动5个不同的进程,每个进程都有各自的延迟,而又不等待其他进程等待延迟完成?我不能使用异步或等待

How can I start 5 different processes, each with their own delay without holding up the other ones while waiting for the delay to finish? I can not use async or await

 foreach(string process1 in _processList)
  {
     // random delay
     Process.Start(process1);
    }

推荐答案

您可以从不同的线程启动每个进程.

You could start every process from a different thread.

foreach (string process1 in _processList)
{
  Thread t = new Thread(() => 
           {
               Thread.Sleep(/*RANDOM NUMBER*/ 5);
               Process.Start(process1);
           });
  t.Start();
}

这样,每个进程在启动之前都会有一个随机计时器,并且不会因为另一个进程的启动而延迟任何进程.

That way each process will have a random timer before it start and no process is delayed for the start of an other process.

如果在您的情况下完全不可能启动线程,我建议将您的进程包装到.bat中,并在此批处理文件中添加睡眠延迟,这样将及时调用所有进程,并且将遵守睡眠延迟

If starting thread is totaly impossible in your situation, i would suggest wrapping your process to a .bat and in this batch file you add the sleep delay this way all the process will be called in time and the sleep delay will be respected.

这篇关于延迟启动流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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