c#注册命令行参数不启动新实例 [英] c# register commandline argument don't start new instance

查看:28
本文介绍了c#注册命令行参数不启动新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Application c:\pinkPanther.exe is running and it is application i wrote in c#. Some other application starts c:\pinkPanther.exe purpleAligator greenGazelle OrangeOrangutan and i would like not to start new instance of c:\pinkPanther.exe with these arguments, but to currently running c:\pinkPanther.exe register it and react to it somehow.

怎么做?

编辑!!!:我对导致混乱的pinkPanther.exe 和ruzovyJeliman.exe 感到非常抱歉-我从我的母语翻译了问题,但错过了:(

EDIT!!!: i'm very sorry about pinkPanther.exe and ruzovyJeliman.exe that caused the confusion - i translated question from my native language and missed it :(

推荐答案

要与应用程序的另一个实例进行通信,您需要某种进程间通信.显然,WCF 是推荐的形式.Net 中的 IPC.你可以用这样的代码来做到这一点(使用 WPF,但 WinForms 会类似):

To communicate with the other instance of the application, you need some sort of inter-process communication. Apparently, WCF is the recommended form of IPC in .Net. You can do that with code like this (using WPF, but WinForms would be similar):

[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<ISingletonProgram>(
            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);
    }
}

这篇关于c#注册命令行参数不启动新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆