1
A
回答
0
Process.GetProcessById(processId, machineName).WaitForExit();
0
使用SysInternals的PSLIST輪詢遠程計算機。
2
對於我Process.GetProcessByID()只是沒有工作(它聲稱它不能連接到機器)。這工作:
public static bool WaitForProcess(int pid, string machine, TimeSpan timeout)
{
// busy wait
DateTime start = DateTime.Now;
while (IsAlive(pid, machine))
{
if (start.Add(timeout).CompareTo(DateTime.Now) <= 0)
return false;
Thread.Sleep(1000);
}
return true;
}
public static bool IsAlive(int pid, string machine)
{
// doesn't work for me (throws "The network path was not found" exception)
//return Process.GetProcessById(pid, @"\\" + machine) != null;
string user;
string domain;
GetProcessInfoByPID(pid, machine, out user, out domain);
return !string.IsNullOrEmpty(user);
}
public static string GetProcessInfoByPID(int PID, string machine, out string User, out string Domain)
{
// copied from http://www.codeproject.com/KB/cs/processownersid.aspx?fid=323674&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2076667
// with slight modifications
ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", machine), connOptions);
manScope.Connect();
User = String.Empty;
Domain = String.Empty;
string OwnerSID = String.Empty;
string processname = String.Empty;
try
{
ObjectQuery sq = new ObjectQuery
("Select * from Win32_Process Where ProcessID = '" + PID + "'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(manScope, sq);
if (searcher.Get().Count == 0)
return OwnerSID;
foreach (ManagementObject oReturn in searcher.Get())
{
string[] o = new String[2];
//Invoke the method and populate the o var with the user name and domain
oReturn.InvokeMethod("GetOwner", o);
//int pid = (int)oReturn["ProcessID"];
processname = (string)oReturn["Name"];
//dr[2] = oReturn["Description"];
User = o[0];
if (User == null)
User = String.Empty;
Domain = o[1];
if (Domain == null)
Domain = String.Empty;
string[] sid = new String[1];
oReturn.InvokeMethod("GetOwnerSid", sid);
OwnerSID = sid[0];
return OwnerSID;
}
}
catch
{
return OwnerSID;
}
return OwnerSID;
}
相關問題
- 1. Windows:等待進程完成
- 2. 等待WMIC完成遠程調用
- 3. Ruby:等待任何子進程完成
- 4. Python沒有等待子進程完成?
- 5. PowerShell腳本等待進程完成
- 6. Perl:等待後臺進程完成
- 7. 等待程序完成
- 8. Java等待線程完成
- 9. 等待popen3過程完成?
- 10. 等待完成處理程序完成
- 11. 等待進程,直到所有子進程完成?
- 12. 等待一個進程完成並執行另一個進程
- 13. Java多線程等待線程完成
- 14. VBScript - 如何讓程序等待進程完成?
- 15. 等待進程
- 16. Plink完成遠程命令執行後等待按鍵
- 17. 等待完成
- 18. 等待完成
- 19. 新線程永不完成等待WebBrowser
- 20. 等待完成Java的過程
- 21. 等待一個完成的過程
- 22. 如何等待所有線程完成?
- 23. 若要完成線程的等待(Android)
- 24. 試圖等待所有線程完成
- 25. ManualResetEvent不等待線程池完成
- 26. 等待多個流程完成預期
- 27. 如何等待卸載程序完成?
- 28. 等待應用程序代碼完成
- 29. 等待線程完成執行
- 30. CMD不會等待完成過程
不知道這是如何得到正確的答案。根據doco http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx當「您正嘗試爲在遠程計算機上運行的進程調用WaitForExit()時,將引發SystemException。方法僅適用於在本地計算機上運行的進程。「 – Simon 2011-09-15 00:36:35