问题
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