[英]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.