c#register命令行参数不启动新实例

应用程序c:\ pinkPanther.exe正在运行,它是我在c#中编写的应用程序。 其他一些应用程序启动c:\ pinkPanther.exe purpleAligator greenGazelle OrangeOrangutan我想不用这些参数启动c:\ pinkPanther.exe的新实例,但是对于当前正在运行的c:\ pinkPanther.exe注册它并以某种方式对它作出反应。

怎么做?

编辑!!!:我很抱歉pinkPanther.exe和ruzovyJeliman.exe引起了混乱 – 我翻译了我的母语问题并错过了它:(

要与应用程序的其他实例进行通信,您需要进行某种进程间通信。 显然, WCF是.Net中推荐的IPCforms 。 您可以使用这样的代码(使用WPF,但WinForms类似):

[ServiceContract] public interface ISingletonProgram { [OperationContract] void CallWithArguments(string[] args); } class SingletonProgram : ISingletonProgram { public void CallWithArguments(string[] args) { // handle the arguments somehow } } public partial class App : Application { private readonly Mutex m_mutex; private ServiceHost m_serviceHost; private static string EndpointUri = "net.pipe://localhost/RuzovyJeliman/singletonProgram"; public App() { // find out whether other instance exists bool createdNew; m_mutex = new Mutex(true, "RůžovýJeliman", out createdNew); if (!createdNew) { // other instance exists, call it and exit CallService(); Shutdown(); return; } // other instance does not exist // start the service to accept calls and show UI StartService(); // show the main window here // you can also process this instance's command line arguments } private static void CallService() { var factory = new ChannelFactory( new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), EndpointUri); var singletonProgram = factory.CreateChannel(); singletonProgram.CallWithArguments(Environment.GetCommandLineArgs()); } private void StartService() { m_serviceHost = new ServiceHost(typeof(SingletonProgram)); m_serviceHost.AddServiceEndpoint( typeof(ISingletonProgram), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), EndpointUri); m_serviceHost.Open(); } protected override void OnExit(ExitEventArgs e) { if (m_serviceHost != null) m_serviceHost.Close(); m_mutex.Dispose(); base.OnExit(e); } } 

这假设您的应用程序是一个WinForms应用程序,因为这将使其更容易保持打开状态。 这是一个非常简单的示例,但它将向您展示基础知识:

  1. 添加对Microsoft.VisualBasic的引用。
  2. 创建一个inheritance自WindowsFormsApplicationBase的Application类。 此基类包含内置机制,用于创建单实例应用程序并使用新参数响应命令行上的重复调用:

     using Microsoft.VisualBasic.ApplicationServices; //omitted namespace public class MyApp : WindowsFormsApplicationBase { private static MyApp _myapp; public static void Run( Form startupform ) { _myapp = new MyApp( startupform ); _myapp.StartupNextInstance += new Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventHandler( _myapp_StartupNextInstance ); _myapp.Run( Environment.GetCommandLineArgs() ); } static void _myapp_StartupNextInstance( object sender, Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e ) { //e.CommandLine contains the new commandline arguments // this is where you do what you want with the new commandline arguments // if you want it the window to come to the front: e.BringToForeground = true; } private MyApp( Form mainform ) { this.IsSingleInstance = true; this.MainForm = mainform; } } 
  3. 你需要在Main()更改的是在新类而不是Application.Run()上调用Run() Application.Run()

     static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false ); MyApp.Run( new MyMainForm() ); } } 

WindowsFormsApplicationBase还具有您可以探索的其他function 。