4

I would like to pass command line arguments of a new instance of my application to one already running (if one exists). So far, I have tried the following:

Program.cs

string[] Arguments = Environment.GetCommandLineArgs();

int iCurrentProcessID = -1;
using (Mutex mutex = new Mutex(true, Arguments[1], out createdNew)) //Finding if application is running or not
{
    Process current = Process.GetCurrentProcess();
    iCurrentProcessID = current.Id;
    if (createdNew)
    {
        Application.Run(Frm1);
    }
    else
    {           
        //  Process current = Process.GetCurrentProcess();
        Process CurrentAutomation = Process.GetProcessById(iCurrentProcessID);

        string[] strArguments = Environment.GetCommandLineArgs();

        if (!string.IsNullOrWhiteSpace(strArguments[2]))
        {
            frmMain.strEndtime = strArguments[2];              
        }

Form.cs

public partial class frmMain : Form
{
    public static string strEndtime;
    //...
}

Values are set correctly in the second instance, but are not set in the first (started earlier) one. How do I pass those values to the other instance of my application?

slawekwin
  • 6,270
  • 1
  • 44
  • 57
Babu
  • 73
  • 1
  • 13
  • what are you trying to do exactly? do you want to set the first application instance's property value to argument of the second instance? – slawekwin Jan 16 '17 at 09:21
  • I want to use the second instance agrument in first instance. – Babu Jan 16 '17 at 09:31
  • try googling something like "c# pass parameters to running application", for example [this](http://stackoverflow.com/questions/3793997/pass-arguments-to-running-application) might help – slawekwin Jan 16 '17 at 09:36
  • I applied all but How can I use arguments thats what I want to know – Babu Jan 16 '17 at 09:43
  • you cannot simply pass values between processes. you have to implement special handling for that, which is relatively easy in winforms, fortunately. do your research in this topic - it has nothing to do with mutexes – slawekwin Jan 16 '17 at 09:49
  • I didn't got so posted here. – Babu Jan 16 '17 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133255/discussion-between-babu-and-slawekwin). – Babu Jan 16 '17 at 10:03

1 Answers1

1

As I know, you can utilize WCF to send values between processes. Your main application expose a WCF service via net.pipe. Check this link for details

Dan Nguyen
  • 1,308
  • 6
  • 17