C# 中的 VB6 LinkExecute 等效项 [英] VB6 LinkExecute equivalent in C#
问题描述
在 VB6 中有一个名为 LinkExecute
的 Form 事件,我可以用它来将两个项目链接在一起.例如,我使用按钮创建项目 A 并将其与项目 B 链接,当我单击项目 A 上的按钮时,项目 B 中的文本框发生了变化.
In VB6 there is a Form event called LinkExecute
witch I can use to link tow projects together. For example I create project A with a button and link it with project B witch has a textbox when I click on the button on project A the textbox in project B changed.
为了简化想法,它链接两个项目并使其中一个项目侦听其他事件,当主项目上发生特定事件时,侦听器在侦听器项目本地触发一个事件.
To simplify the idea it link the tow projects and make one of them listen to the other events and when a specific event occur on the main project the listener fire an event locally on the listener project.
两个项目都是 WinForms 并在同一台机器上运行.
Both projects are WinForms and run on the same machine.
项目A
Private Sub Command1_Click()
On Error Resume Next
Text1.LinkTopic = "Project1|SYSTEM"
Text1.LinkItem = "TEXTSource"
Text1.LinkMode = vbLinkManual
Text1.LinkRequest ' "Hello World"
Text1.LinkExecute "Hello World"
DoEvents
End Sub
Public Sub Form_Load()
End Sub
项目 B
Private Sub Command1_Click()
Label1.Caption = Val(Label1.Caption) + 1
End Sub
Private Sub Form_LinkClose()
List1.AddItem "Form_LinkClose"
Command1_Click
End Sub
Private Sub Form_LinkError(LinkErr As Integer)
List1.AddItem "form_LinkError"
Command1_Click
End Sub
Private Sub Form_LinkExecute(CmdStr As String, Cancel As Integer)
List1.AddItem "Command " & CmdStr & " has been received"
Cancel = False
Command1_Click
End Sub
Private Sub Form_LinkOpen(Cancel As Integer)
List1.AddItem "Form_LinkOpen"
Cancel = False
Command1_Click
End Sub
Private Sub Form_Load()
List1.Clear
Command1_Click
End Sub
Private Sub PictureSource_LinkClose()
List1.AddItem "PictureSource LinkClose"
Command1_Click
End Sub
Private Sub PictureSource_LinkError(LinkErr As Integer)
List1.AddItem "PictureSource LinkError: Error = " & LinkErr
Command1_Click
End Sub
Private Sub PictureSource_LinkNotify()
List1.AddItem "PictureSource LinkNotify"
Command1_Click
End Sub
Private Sub PictureSource_LinkOpen(Cancel As Integer)
List1.AddItem "PictureSource LinkOpen"
Command1_Click
End Sub
那么 C# 中 LinkExecute
的等价物是什么,或者我如何在 C# 中做同样的事情?
So what is the equivalent to LinkExecute
in C# or how can I do the same in C#?
推荐答案
DDE 是一种较旧的进程间通信协议,它严重依赖于在应用程序之间来回传递 Windows 消息.其他更现代、更强大的进程间通信技术是可用的,例如 进程间通信为了让两个项目交换事件,他们必须就这些事件的沟通方式达成一致.有许多不同的方法可以做到这一点,具体使用哪种方法可能取决于体系结构和上下文.
DDE is an older interprocess communication protocol that relies heavily on passing windows messages back and forth between applications. Other, more modern and robust, techniques for interprocess communication are available like Inter-process communication In order for two projects to exchange events, they must agree on how these events are communicated. There are many different ways of doing this, and exactly which method to use may depend on architecture and context.
您还可以查找一些常用技术,例如 files(从公共文件读取)、命名管道, 队列 (MSMQ),使用 TCP/UDP 套接字连接, 使用 WebServices、WCF 或 Restful Web Service,远程过程调用 (RPC),从数据库中的常见条目.(不推荐)、窗口消息 和 共享内存.当您自己实现这两个应用程序时,您可以选择使用您喜欢的任何 IPC 方法.网络套接字和更高级别的基于套接字的协议,如 HTTP、XML-RPC 和 SOAP,因为它们允许您在不同的物理机器上运行应用程序.
You can also look for some of the common techniques used like files(Reading from a common file), Named Pipes, Queues (MSMQ), Use TCP/UDP socket connection, Use WebServices, WCF or Restful Web Service, Remote Procedure Calls (RPC), Reading from a common entry in a db. (Not recommended), window messages and shared memory. As you are implementing both applications yourself you can chose to use any IPC method you prefer. Network sockets and higher-level socket-based protocols like HTTP, XML-RPC and SOAP, as they allow you do run the applications on different physical machines as well.
我更喜欢使用 MSMQ,因为它可以保留在不同机器上运行进程的能力
I would prefer to use MSMQ as it would preserve the ability of having processes in different machines
- MSMQ 允许您不会丢失消息(以防 RECEIVER 关闭)
- MSMQ 允许您在同一台机器或不同的机器上拥有进程机器
- Windows 服务使您能够启动/停止轻松处理
- Windows 服务可以监控我的 SNMP,并且通常它们可以轻松与 Windows 管理工具集成.
编辑
答案参考取自 Listen for events in另一个应用程序,发送/接收消息至/来自两个正在运行的应用程序和NDde: CodePlex
这篇关于C# 中的 VB6 LinkExecute 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!