Send data to WPF singleton application from other

2019-07-20 02:54发布

问题:

I have a WPF singleton application, wherein only one instance is running at any time, if user tries to launch another instance, we check if its already running then we kill this new process and bring to front the existing one.

Now, we have a requirement to open/access and send message (set of arguments) to this WPF application from another process which can be xls, word, or another standalone application. We also want to make sure that if the process is already running, that process should hear the message and respond.

Any idea how we can implement it in our WPF application

Thanks

回答1:

The probably most interoperable way on Windows is using the WM_COPYDATA windows message to transfer data between applications.

You can find an example here and another one for WPF right here.



回答2:

MSDN provides a pretty good article on the communication option in .NET.

I personally had this problem twice on 2 differents applications.

First time we used a TCP based connection. This is really efficient, but you need to code a lot to make it works. What we observe is that it is hard to define a standard of communication because developpers are not used to work with TCP connection (in my context at least).

Thus the second time we use a WCF based connection. Our singleton WPF application host the connection, and the other application connect to it when available. It was a better experience as manage host service is pretty natural for most developpers (used to work with web services). Another avantage is that you can reach you WPF singleton through a web connection in the end if required.

Hope it helps.