Preface
If multi-process communication is required in WPF, a recommended method is WCF because WCF is RPC computing. Let’s first talk about RPC (Remote Procedure Call) remote procedure call, which uses specific protocols, including tcp, http, etc. to call other processes. Please see moreBaidu
Now I won’t tell you how to use WCF. The following is about using the removal method. You need to know that dotnet remoting is an outdated technology. It is recommended to use wcf. However, wcf is difficult to deploy. For high performance requirements or if you want to use it quickly, it is recommended to use remoting. How to use is very simple
First, create three projects, one is placed on the other two libraries that need to be used, one is the server and the other is the client. The client can call the server, and the client and the server are two different processes, so they can be called across processes.
The method is as follows:
First create a simple project, the library project RemoteObject, which has only one class
public class RemoteCalculator : MarshalByRefObject { public const int Port = 13570; public int Add(int a, int b) { return a + b; } }
Note that this class needs to inherit MarshalByRefObject. This class is referenced in two processes. The client does not implement this class, so the client can also use this class interface. The specific method of calling this class runs on the server, and the result is returned through tcp or http.
The main code of the client is to connect to the server and then access the library's add function, but this function is not run on the client. It calls the server through tcp and lets it run.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { if (_channel == null) { (""); _channel = new TcpChannel(); (_channel, true); } var calculator = (RemoteCalculator) (typeof(RemoteCalculator), "tcp://" + "127.0.0.1" + ":" + + "/RemoteCalculator"); ((1, 2)); }
The name of the server is CalcnsMnlhzydYeuitcCddhxvlhm , which mainly opens the connection and executes the functions sent by the client.
static void Main(string[] args) { new Thread(() => { _channel = new TcpChannel(); (_channel, true); (typeof(RemoteCalculator), "RemoteCalculator", ); }).Start(); while (true) { (); } } private static TcpChannel _channel;
Need to pay attention, the client needs to open the server first when clicking the button, use this code (""); and then create tcp to tell the connection between the server through tcp. Then get the Calculator class from the server. In fact, this class is not implemented now, and the calling function needs to be sent to the server.
The server needs to open TcpChannel, and then you need to define the calling class.(typeof(RemoteCalculator), "RemoteCalculator", );
, One parameter of this function is the registered class. The second function is the name of the class called. Generally, the name of the class can be used. The last parameter can be connected to an instance in one connection. Therefore, the class in the library cannot be passed in the constructor.
The last RemoteCalculator called "tcp://" + "127.0.0.1" + ":" + + "/RemoteCalculator" The last RemoteCalculator is the second function registered by the server.
So what is the function of this function? because x64 The program cannot be called x86 The library,So you can use this method x64 的程序调用其他平台The library,because进程运行的平台不一样,But the communication is the same。
I haven't used other functions, so I use the service to open it and call its functions, so if you encounter problems, don't ask me. If the code cannot be run, you can send me an email and I will send you the source code
Code download:Click here
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.