简体   繁体   中英

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.

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

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

为什么不使用套接字(客户端和服务器)的普通旧TCP / IP。

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. eg 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.

I would go with WindowsFormsApplicationBase class (from Microsoft.VisualBasic assembly) with the following code in my Program.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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM