Start process på en anden computer og afvent at den bliver færdig
Jeg har denne dummy app placeret på PC1 i “d:\temp\Wait10Sec.exe”class Program {
static void Main(string[] args) {
for(int i=0;i<10;i++) {
System.Threading.Thread.Sleep(1000);
}
}
}
Fra PC2 afvikler jeg denne kode:
namespace ConsoleApplication3 {
internal class Program {
private static void Main(string[] args) {
Runner runner = new Runner();
runner.Run();
}
}
public class Runner {
private bool isComplete;
public void Run() {
ConnectionOptions theConnection = new ConnectionOptions();
theConnection.Username = "admin";
theConnection.Password = "pw";
ManagementScope theScope = new ManagementScope("\\\\PC1\\root\\cimv2",
theConnection);
theScope.Connect();
ManagementOperationObserver responseHandler = new
ManagementOperationObserver();
responseHandler.Completed += new CompletedEventHandler(Done);
responseHandler.ObjectReady += new ObjectReadyEventHandler(NewObject);
isComplete = false;
ManagementClass mc = new ManagementClass(theScope, new
ManagementPath("Win32_Process"), new ObjectGetOptions());
// Invoke method asynchronously
ManagementBaseObject inParams = mc.GetMethodParameters("Create");
inParams["CommandLine"] = "d:\\temp\\Wait10Sec.exe";
inParams["CurrentDirectory"] = "d:\\temp";
object res = mc.InvokeMethod("Create", inParams, null);
// Do something while method is executing
while (!isComplete) {
Thread.Sleep(1000);
}
Console.WriteLine("Done");
}
private void NewObject(object sender, ObjectReadyEventArgs e) {
//do nothing
}
private void Done(object sender, CompletedEventArgs e) {
isComplete = true;
}
}
}
Processen Wait10Sec.exe start korrekt på PC1, men Runner.Done(object sender,
CompletedEventArgs e) bliver kaldt med det samme, og altså ikke efter 10 sekunder.
Mit spørgsmål er derfor "Hvordan kan jeg blive notificeret når Wait10Sec.exe processen er færdig"?