SRJ
SRJ

Reputation: 29

How can I send data between two programs in C#?

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.

Upvotes: 2

Views: 5774

Answers (5)

jsoques
jsoques

Reputation: 76

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

Upvotes: 1

Regent
Regent

Reputation: 5602

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);
        }
    }
}

Upvotes: 0

Binary Worrier
Binary Worrier

Reputation: 51711

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.

Upvotes: 0

Pieter Germishuys
Pieter Germishuys

Reputation: 4886

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

Upvotes: 5

Vlad
Vlad

Reputation: 35594

You should use IPC. See IPC Mechanisms in C# - Usage and Best Practices for some useful links.

Upvotes: 4

Related Questions