command line - c# register commandline argument don't start new instance -
application c:\pinkpanther.exe running , application wrote in c#. other application starts c:\pinkpanther.exe purplealigator greengazelle orangeorangutan , not start new instance of c:\pinkpanther.exe these arguments, running c:\pinkpanther.exe register , react somehow.
how it?
edit!!!: i'm sorry pinkpanther.exe , ruzovyjeliman.exe caused confusion - translated question native language , missed :(
to communicate other instance of application, need sort of inter-process communication. apparently, wcf recommended form of ipc in .net. can code (using wpf, winforms similar):
[servicecontract] public interface isingletonprogram { [operationcontract] void callwitharguments(string[] args); } class singletonprogram : isingletonprogram { public void callwitharguments(string[] args) { // handle 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 , exit callservice(); shutdown(); return; } // other instance not exist // start service accept calls , show ui startservice(); // show main window here // can process 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); } }
Comments
Post a Comment