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. 等待并行远程进程完成bash ssh
- 3. 如何等待多个远程进程(通过ssh)完成?
- 4. 等待WMIC完成远程调用
- 5. 如何等待远程exe完成 - c#
- 6. Python Gtk2&Vte等待进程Pid完成
- 7. Python没有等待子进程完成?
- 8. PowerShell脚本等待进程完成
- 9. Perl:等待后台进程完成
- 10. 等待进程完成运行
- 11. Java:等待所有子进程完成
- 12. Ruby:等待任何子进程完成
- 13. 等待UI线程完成
- 14. 等待线程完成python
- 15. 等待程序完成
- 16. 等待popen3过程完成?
- 17. 等待过程完成
- 18. Java等待线程完成
- 19. 等待两个子进程完成但不一定先等待
- 20. 等待完成处理程序完成
- 21. 用lldb远程调试 - 等待进程
- 22. 等待一个进程完成并执行另一个进程
- 23. 等待进程,直到所有子进程完成?
- 24. Java多线程等待线程完成
- 25. 使主程序等待线程完成
- 26. VBScript - 如何让程序等待进程完成?
- 27. 等待进程
- 28. Plink完成远程命令执行后等待按键
- 29. MVC4不显眼验证不等待远程验证完成
- 30. 欧芹验证不等待远程验证AsyncValidate完成
不知道这是如何得到正确的答案。根据doco http://msdn.microsoft.com/en-us/library/fb4aw7b8.aspx当“您正尝试为在远程计算机上运行的进程调用WaitForExit()时,将引发SystemException。方法仅适用于在本地计算机上运行的进程。“ – Simon 2011-09-15 00:36:35