简体   繁体   English

如何在C#中的两个程序之间发送数据?

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

I have two applications --> App1 and App2. 我有两个应用程序 - > App1和App2。 App1 opens App2 by passsing some command line arguments using System.Diagnostic.Process(). App1通过使用System.Diagnostic.Process()传递一些命令行参数来打开App2。 The user now accesses App2. 用户现在可以访问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. 但是,当用户在App1中更改某些命令参数时,我需要打开现有的应用程序(App2)而不使用新参数关闭它。

How can I do this? 我怎样才能做到这一点?

Any feedback would be helpful. 任何反馈都会有所帮助。

Another option might be a WCF based solution. 另一种选择可能是基于WCF的解决方案。 See WCF Chat Sample 请参阅WCF聊天示例

You should use IPC. 你应该使用IPC。 See IPC Mechanisms in C# - Usage and Best Practices for some useful links. 有关一些有用的链接,请参阅C#中的IPC机制 - 用法和最佳实践

为什么不使用套接字(客户端和服务器)的普通旧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). 在.net中执行预先打包的方法称为Remoting ,它内置于框架中并允许IPC(进程间调用)。

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). App1将参数写入文本文件(XML,Delimited,您真正的选择)。

Have a timer on App2 that wakes up every 10th of a second and checks to see if there is a new parameter file. 在App2上有一个定时器,每隔10秒唤醒一次,并检查是否有新的参数文件。 If so it consumes it and delets the file. 如果是这样,它会消耗它并删除文件。

UPDATE 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. 正如John Saunders正确指出的那样,Remoting已被WCF取代,但Remoting上仍有大量信息,这可能不是一个糟糕的开始。

I would go with WindowsFormsApplicationBase class (from Microsoft.VisualBasic assembly) with the following code in my Program.cs file: 我将使用WindowsFormsApplicationBase类(来自Microsoft.VisualBasic程序集)和Program.cs文件中的以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在两个C#程序之间发送大量数据 - Sending large volume of data between two C# programs 我做了两个程序,一个 C++ 控制台和一个 C#/WPF windows 桌面。 我该如何整合它们? - I made two programs, one C++ console and one C#/WPF windows desktop. How can I integrate them? 如何使用C#查找程序列表? - How can i find programs list with C#? 如何在C#中计算两个字符串之间的相似度? - How can I calculate similarity between two strings in C#? C#如何发送带有数据OnDragDrop的文本 - C# How can I send a text with the data OnDragDrop 如何在Android C#中通过USB端口发送数据? - How can i send data by USB port in Android C#? 使用JSON在两个C#程序之间传递异常 - Passing exceptions between two C# programs using JSON C#如何在类实例之间保留数据? - C# how can I preserve data between class instances? 如何在C#Windows应用程序中使用Google Maps获取两个位置之间的旅行时间数据? - How can I get travel time data between two locations using Google Maps in my C# Windows application? 有没有可以分析c#程序中变量之间依赖关系的工具? - Is there any tool that can analyze the dependencies between variables in c# programs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM