start processes with a delay

自古美人都是妖i 提交于 2021-02-17 06:14:36

问题


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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/31566876/start-processes-with-a-delay

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!