2013-12-11 199 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.这是一个好主意,但有一种情况,用户有一个阻止访问进程树的应用程序,在这种情况下它不起作用。 –