Async process start and wait for it to finish

后端 未结 5 1102
小鲜肉
小鲜肉 2020-11-27 15:31

I am new to the thread model in .net. What would you use to:

  1. start a process that handles a file (process.StartInfo.FileName = fileName;)
  2. wait for th
5条回答
  •  暖寄归人
    2020-11-27 16:09

    Try the following code.

    public void KickOffProcess(string filePath) {
      var proc = Process.Start(filePath);
      ThreadPool.QueueUserWorkItem(new WaitCallBack(WaitForProc), proc);
    }
    
    private void WaitForProc(object obj) {
      var proc = (Process)obj;
      proc.WaitForExit();
      // Do the file deletion here
    }
    

提交回复
热议问题