views:

512

answers:

6

I have two applications --> App1 and App2. App1 opens App2 by passsing some command line arguments using System.Diagnostic.Process(). The user now accesses App2.

However, when the user changes some command arguments in App1, I need to open the existing aplication (App2) without closing it using the new parameters.

How can I do this?

Any feedback would be helpful.

+4  A: 

You should use IPC. See http://stackoverflow.com/questions/56121/ipc-mechanisms-in-c-usage-and-best-practices for some useful links.

Vlad
Technically, you're restating the original question because IPC *is* communication between two different processes. By your link, it's clear you're referring to IPC with WCF, which is a good solution to this problem.
280Z28
A: 

Here are some articles which discuss the same:

http://www.ai.uga.edu/mc/SingleInstance.html
http://www.codeproject.com/KB/cs/CSSIApp.aspx
deostroll
A: 

What your looking to do is not straight forward. The pre-packaged way to do it in .net is called Remoting, it's built into the framework and allows IPC (Interprocess calls).

Depending on your level of experience, you may be better rolling your own simplified version of this. e.g. Have the two programs pass data using files.

App1 writes parameters to a text file (XML, Delimited, your choice really).

Have a timer on App2 that wakes up every 10th of a second and checks to see if there is a new parameter file. If so it consumes it and delets the file.

UPDATE
As correctly pointed our by John Saunders, Remoting has been superseeded by WCF, however there is still lots of information out there on Remoting and it might not be a bad place to get started.

Binary Worrier
Actually, remoting has been deprecated in favor of WCF.
John Saunders
@John: Yes, I seem to remember that being announced. Why am I never invited to these meetings where these decisions are made? It might be an oversight by some overworked Microsoft minion, or some day soon all those old, out of date invitations will find me and come flooding into my mail box . . . any day now . . .
Binary Worrier
WCF has a somewhat steeper learning curve, I believe, but the desired protocol looks pretty simple and there should be good sample code out there for similarly simple communication.
Greg D
@Greg: simple use of WCF is very easy - just some terminology differences.
John Saunders
@Binary: I cannot locate the statement about Remoting being deprecated. It's pretty obvious, though. Once most of your functionality has been replaced, there's not much room left.
John Saunders
@Binary: found at http://msdn.microsoft.com/en-us/library/72x4h507.aspx: "This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. Distributed applications should now be developed using the Windows Communication Foundation (WCF)."
John Saunders
+4  A: 

Another option might be a WCF based solution. See WCF Chat Sample

PieterG
+1 because on the current version of .Net, WCF is the choice when doing IPC.
Bruno Brant
A: 

I would go with WindowsFormsApplicationBase class (from Microsoft.VisualBasic assembly) with the following code in myProgram.cs file:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace TestSolution
{
    sealed class Program : WindowsFormsApplicationBase
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] commandLine)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var program = new Program()
            {
                IsSingleInstance = Properties.Settings.Default.IsSingleInstance
            };

            // Here you can perform whatever you want to perform in the second instance

            // After Program.Run the control will be passed to the first instance    
            program.Run(commandLine);
        }

        protected override void OnCreateMainForm()
        {
            MainForm = new ImportForm();
        }

        protected override bool OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            // This code will run in the first instance

            return base.OnStartupNextInstance(eventArgs);
        }
    }
}
Regent
+1  A: 

Why not plain old TCP/IP using sockets (client and server).

jsoques