不確定問題到底是什麼,但是對於它的價值,我有一個運行虛擬機測試並需要重新啓動它們的測試框架。 (通過WMI)重啓盒子後,我等待一個ping失敗,那麼一個ping成功(使用System.Net.NetworkInformation.Ping
如其他人所說),那麼我需要等到Windows準備就緒:
private const int RpcServerUnavailable = unchecked((int)0x800706BA);
private const int RpcCallCancelled = unchecked((int)0x80010002);
public bool WindowsUp(string hostName)
{
string adsiPath = string.Format(@"\\{0}\root\cimv2", hostName);
ManagementScope scope = new ManagementScope(adsiPath);
ManagementPath osPath = new ManagementPath("Win32_OperatingSystem");
ManagementClass os = new ManagementClass(scope, osPath, null);
ManagementObjectCollection instances = null;
try
{
instances = os.GetInstances();
return true;
}
catch (COMException exception)
{
if (exception.ErrorCode == RpcServerUnavailable || exception.ErrorCode == RpcCallCancelled)
{
return false;
}
throw;
}
finally
{
if (instances != null)
{
instances.Dispose();
instances = null;
}
}
}
這是一個有點天真,但它的工作:)
不知道我得到「何時」,但如果一臺機器脫機不會有點難以告訴它重新啓動? – 2011-01-26 21:28:36