C # simple inter-process communication way

Just want to achieve a simple process to build communication, what is fast implementations?

1 process singleton demand / Windows messages IMessageFilter

If demand is to achieve a singleton process, when you start the second process, looking forward to the first window automatically process evokes.
Can be found in:
C # / WPF only start a process instance - J. sun cat - blog Park

But there is a problem, if realized Minimize to tray ( WPF / system tray supports the .NET Core WPF ) this function, then the wake will fail.
The reason: after the window is hidden, will not receive the message windows. For more details and how to do it, you can see: C # to send a message to the windowless process _ _C # tutorial script home
but the implementation depends on the WinForm, WPF is not in force. ( The Message Filters in WPF? )

So, if a WinForm program, you can IMessageFilterachieve a simple inter-process communication, send a command or OK to.

2 WPF program

WPF can use the remote agent to achieve.
RemotingServices class (System.Runtime.Remoting) | Microsoft Docs

Simple achieve the following:

  • Server (to be called end)
// 服务端可以被代理调用的类
internal class OneServiceRemoteProvider : MarshalByRefObject
{
    public string DoSomething(string parameter)
    {
        // do something
    }
}
// 服务端初始化代码:
public const string ServiceIpcPortName = "B7262FBA-0498-46BF-B4D5-E6D54A1A636B"; // 定义一个 IPC 端口
var remoteProvider = new OneServiceRemoteProvider();

// 将 remoteProvider/OneServiceRemoteProvider 设置到这个路由,你还可以设置其它的 MarshalByRefObject 到不同的路由。
RemotingServices.Marshal(remoteProvider, "one");
ChannelServices.RegisterChannel(new IpcChannel(ServiceIpcPortName), false);
  • The client (end call)
var oneRemoteProvider = (OneServiceRemoteProvider)Activator.GetObject(typeof(OneServiceRemoteProvider), $"ipc://{ServiceIpcPortName}/one");
// 在这里就可以通过 oneRemoteProvider 实现对服务端的远程调用了。

Other issues:
1 parameter can only deliver basic type, it does not support events and delegates, if you pass a reference type, you need to be serialized.
2 where the server and the client is only logical concept, as long as know each other's IPC port number and routing address, you can achieve call.

3 .net core / .net core WPF program

.net core no longer provide support for Remoting, the .NET Framework Technologies ON the .NET unavailable Sign of the Core - the .NET Core | in the Microsoft Docs
therefore, have to think of other ways, Microsoft's official recommendation is Pipe. System.IO.Pipes the Namespace | in the Microsoft Docs
in under .net framework can also be used.

Pipe Example of use:

// to be continued

c# - Example of Named Pipes - Stack Overflow


Original link:
between C # simple process of communication - J. sun cat - blog Park

Guess you like

Origin www.cnblogs.com/jasongrass/p/11794247.html