在 C# 中的应用程序之间传递信息 [英] Passing Information Between Applications in C#

查看:18
本文介绍了在 C# 中的应用程序之间传递信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部.首先,我知道问题http://en.wikipedia.org/wiki/Inter-process_communication

其中一些提供双向通信,一些需要考虑系统安全(防病毒和防火墙限制,您需要在其设置中将应用添加为例外).

通过WM_COPYDATA发送消息只能通过SendMessage完成,不支持PostMessage,表示通信是同步的.

使用 outproc 单例 COM 对象是另一种方式,它不像其他方式那么简单,而且两者都是应用程序.必须在相同的安全上下文中运行才能访问相同的 COM 对象.

启动一个单独的应用程序可能会因为通信方法或您可以传递的数据类型存在一些限制,但将它们分开也可以保护它们免受故障的影响(应用程序崩溃不会关闭另一个).

如果这两个部分总是在同一台 PC 上运行,使用其中之一作为 dll[inproc] 会更简单.使用其他技术,如 Socket、Remoting、WCF 将为您提供更多的通信灵活性,即两部分可以在不同的 PC 上运行,只需稍作修改...

All. Firstly I am aware of the question Send data back to .exe from dll, however the answers there leave too many open ends for me and as I have no experience of what I am attempting I feel a new question is warranted.

I have an existing C# [All WinForms here] app that heavily relies on SQL. We have been asked (by clients) to provide an SQL Editor and library that can be used to develop and test SQL, but that can also be used to paste directly back in to the main application. The new SQLEditor is a multi-threaded application that parses and executes TSQL. I now have some things to consider; what is the best way to launch this second application from the main application::

  1. Make the second app into a DLL and load into the main project, call the second app as a new form (SqlEditor sqlEd = new SqlEditor() etc.)? What are the implication in terms of thread bombardment, would I need [STAThread] - as I want both multithreaded apps to be available and active at the same time.

  2. To launch as a separate .exe from the main application?

Depending on you advice; in either of the above cases - what is the best way I can pass information back to the main application from a click event in the second application whilst they are still both running and active [WCF, ApplicationDomains etc.]? Would the Observer design pattern come in to play here?

To make this question a little more appealing, here is the SQL Editor:

I plan to have a button which pastes the selected SQL back into the main application.

I am also aware that there are multiple questions here - which I apologise for. Thanks very much for your time.

解决方案

You have multiple choices for IPC(Inter Process Communication) such as: Mailslot, NamedPipe, Memory Mapped File, Socket, Windows Messaging, COM objects, Remoting, WCF... http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx http://en.wikipedia.org/wiki/Inter-process_communication

Some of them provide two-way communication, some needs considaration about system security(antivirus & firewall restrictions, you need to add the application as an exception in their settings).

Sending message through WM_COPYDATA can be done just by SendMessage, PostMessage is not supported, it means that the communication is sync.

Using outproc singleton COM object is another way, its not as simple as the other ways and both app. must be run on the same security context to access the same COM object.

Launching a separate application can coz some restrictions in communication methods or types of data you can pass, but separation of them will also protect them from their failures(App. crash will not close the other one).

If the two parts are always run on the same PC, using one of them as dll[inproc] is simpler. Using other techniques such as Socket, Remoting, WCF will provide you with more flexibility for communication, i.e. the two parts can run in different PCs with minor modifications...

这篇关于在 C# 中的应用程序之间传递信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆