2013-12-11 198 views
2

我的C#項目出現問題。我到了那裏2個應用程序:執行應用程序關閉執行程序應用程序

  • 執行器的應用程序,我會叫迷你發射器
  • 執行的應用程序,我會打電話給發射

我的問題是:我想通過微型發射器來運行我的啓動和在啓動器應用程序關閉迷你啓動器的顯示事件。 我的迷你啓動器就像啓動畫面,但具有升級啓動器等其他功能。它是我的執行代碼:

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.WorkingDirectory = "My Directory" 
startInfo.FileName = "My App"; 
try 
{ 
    using (Process exeProcess = Process.Start(startInfo)) 
    { 
     exeProcess.(); 
    } 
} 
catch 
{ 
    ... 
} 
+1

你是如何從第一個啓動第二個應用程序? – JotaBe

回答

2

看一看的Mutex類。命名的模式爲應用程序提供了一種相互發送信號的方式。
以下示例顯示了兩個控制檯應用程序。該TestMutexLauncher應用程序啓動TestMutex應用:

using System; 
using System.Diagnostics; 
using System.Threading; 

namespace TestMutexLauncher 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var p = Process.Start("TestMutex"); 
      Console.WriteLine("Waiting for other process to release the mutex."); 
      Thread.Sleep(1000); // maybe p.WaitForInputIdle is an alternative for WinForms/WPF 
      Mutex mutex = null; 
      for (int i = 0; i < 100; i++) 
      { 
       if (Mutex.TryOpenExisting("MyUniqueMutexName", out mutex)) 
        break; 
       Thread.Sleep(100); 
      } 
      if (mutex != null) 
      { 
       try 
       { 
        mutex.WaitOne(); 
        mutex.ReleaseMutex(); 
       } 
       finally 
       { 
        mutex.Dispose(); 
       } 
      } 
     } 
    } 
} 

啓動器應用程序啓動過程,並等待在另一個進程中創建一個互斥。如果它可以在指定的時間範圍內獲得互斥量的所有權,它將等待互斥量的所有權。之後,它釋放並處置互斥體。
啓動的應用程序的第一個任務是創建互斥鎖,執行初始化操作,然後釋放互斥鎖。

using System; 
using System.Threading; 

namespace TestMutex 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (var mutex = new Mutex(true, "MyUniqueMutexName")) 
      { 
       // Do something 
       for (int i = 0; i < 10000; i++) 
        Console.Write("."); 
       Console.WriteLine(); 
       Console.WriteLine("Press enter..."); 
       Console.ReadLine(); 
       mutex.ReleaseMutex(); 
      } 
      for (int i = 0; i < 10000; i++) 
       Console.Write("."); 
      Console.WriteLine(); 
      Console.WriteLine("Press enter..."); 
      Console.ReadLine(); 

     } 
    } 
} 
+0

我會檢查這一個謝謝。 –

+0

你能舉個例子嗎? –

+0

@ArturSzymański:我編輯了我的答案並提供了一個示例。 – Markus

1

您可以從當前正在運行的項目調用另一個項目可執行文件,然後關閉應用程序。

class Program 
    { 
     static void Main() 
     { 
     // 
     // Open the application "application" that is in the same directory as 
     // your .exe file you are running. 
     // 
     Process.Start("example.txt"); 
// or for another directory you need to specify full path 
Process.Start("C:\\"); 
     } 
    } 
2

首先,我建議你考慮:

1)他們真的需要是單獨的應用程序?

2)如果是這樣,爲什麼在啓動器加載後MiniLauncher不能自己關閉?

但是,如果你一定要做到這樣,那麼你正在尋找的代碼是這樣的:

private void OnShow() 
{ 
    var target = Process.GetProcessesByName("MiniLauncher.exe").FirstOrDefault(); 

    if (target != null) 
    { 
     // any other checks that this is indeed the process you're looking for 
     target.Close(); 
    } 

} 
+0

1.我的迷你啓動器必須是另一個應用程序,因爲MiniLauncher會在需要時更改啓動器的文件。 –

+0

2.我的Launcher應用程序的初始化時間將持續幾秒鐘,用戶必須知道當前發生的事情。 –

+0

3.這是一個好主意,但有一種情況,用戶有一個阻止訪問進程樹的應用程序,在這種情況下它不起作用。 –