积极答复者
Process.ID获取有时对,有时不对。

问题
-
我有个程序,Task[] C线程启动进程C1,使用的Process.Start()方法启动的,然后用Process.ID获取的这个进程的PID。可是这个PID有时候获取的是对的,有时候获取的是错的,请问这是什么原因造成的?
int processId=-1; bool threadIsOk = false; Process pr = new Process(); pr.StartInfo.CreateNoWindow = true; pr.StartInfo.UseShellExecute = false; pr.StartInfo.FileName = Application.StartupPath + "\\rar.exe"; //传进去的名字要从路径修改成文件名字。 //string fileattr = md5FileAttr(textBox1.Text.ToString() + "\\" + md5filename + ".rar"); //fileNames.Add(md5FileName); pr.StartInfo.Arguments = commandLine + " \"" + selectPath + "\\" + md5FileName + "\" \"" + filePath + "\""; pr.Start(); processId = pr.Id; pr.WaitForExit(); while(!threadIsOk) { try { Process p = Process.GetProcessById(processId); } catch (Exception ex) { if (ex.Message.Contains("未")) { threadIsOk = true; //执行下一步程序 } else { Thread.Sleep(1000); } } }
答案
-
您好 龍五,
>> "可是这个PID有时候获取的是对的,有时候获取的是错的"
请问您能不能发现一些获取是错的规律出来?
>> boolthreadIsOk = false;
threadIsOk是在什么地方设置为true的?
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 CaillenModerator 2016年2月16日 1:48
- 取消答案标记 CaillenModerator 2016年2月16日 1:48
- 已标记为答案 龍五 2016年4月1日 10:11
-
- 已标记为答案 CaillenModerator 2016年2月16日 1:48
全部回复
-
您好 龍五,
>> "可是这个PID有时候获取的是对的,有时候获取的是错的"
请问您能不能发现一些获取是错的规律出来?
>> boolthreadIsOk = false;
threadIsOk是在什么地方设置为true的?
Best Regards,
Li Wang
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 CaillenModerator 2016年2月16日 1:48
- 取消答案标记 CaillenModerator 2016年2月16日 1:48
- 已标记为答案 龍五 2016年4月1日 10:11
-
- 已标记为答案 CaillenModerator 2016年2月16日 1:48