2012-05-01 80 views
-2

所以我發現啓動另一個應用程序,但沒有新的進程

System.Diagnostics.Process.Start() 

但是,後來我想關閉應用程序,它開始工作(這是需要我啓動連接到服務器,啓動遊戲,並關閉自身),所以它不起作用。 我的應用程序:

public partial class MainWindow : Window 
{ 
    Process.Start("xnagame.exe", "12345678"); 
    this.Close(); 
} 
+2

'應用程序==過程' –

+4

另外,什麼「不工作」?發生了什麼,你想要什麼? –

+1

也許「不起作用」是指代碼不能編譯:) – Steve

回答

1

名稱的過程,所以你有一個手柄將其關閉。並非所有進程都優雅地關閉。

 try 
    { 
     Process myProcess; 
     myProcess = Process.Start("Notepad.exe"); 
     // Display physical memory usage 5 times at intervals of 2 seconds. 
     for (int i = 0;i < 5; i++) 
     { 
      if (!myProcess.HasExited) 
      { 
       // Discard cached information about the process. 
       myProcess.Refresh(); 
       // Print working set to console. 
       Console.WriteLine("Physical Memory Usage: " 
            + myProcess.WorkingSet.ToString()); 
       // Wait 2 seconds. 
       Thread.Sleep(2000); 
      } 
      else { 
       break; 
      } 
     } 

     // Close process by sending a close message to its main window. 
     myProcess.CloseMainWindow(); 
     // Free resources associated with process. 
     myProcess.Close(); 

    } 
    catch(Exception e) 
    { 
     Console.WriteLine("The following exception was raised: "); 
     Console.WriteLine(e.Message); 
    } 
+2

嗯,我想OP想關閉啓動過程的應用程序,而不是啓動過程 – Steve

+0

@Steve OK,我想我同意你的解釋。但尚不清楚。我會放棄一天。 – Paparazzi

+0

Sory被誤解了。這是我想要的。 –

0

使用Process.Start(ProcessStartInfo)過載,而UseShellExecute屬性設置爲true

相關問題