简体   繁体   中英

Communication between two winform application using WCF?

I have two different winform application, App1 and app2. App1 calls the exe of app2 (using DOS command window) and sends a message to start the app2. The app2 starts executing and once it finishes its task it sends back the message to app1 that the execution was successful. How can I acheieve this functionality using WCF. earlier the same code was written in foxpro and this finc was achieved using memory management.

I think what you want is peer-to-peer communication, where 2 applications (which may or may not be running on the same machine) send each other messages asynchronously. This is the way chat programs such as MSN Messenger work.

There's a "simple" tutorial about peer-to-peer communication using WCF at MSDN .

Mind you, this is not as easy as it sounds. You may prefer to just send messages using Windows' SendMessage .

This is just conceptual how to achieve this:

You need to implement WCF service. There are many ways how to do this task. One of this should be like this.

App1 calls service method and tell that app2 need to executed. App1 could than wait for the response.

App2 from time to time pings service to see if it needs to be executed. App2 finished its job and call service method to tell that it was done.

App1 will get the response when it was finished.

Other option is not to implement request/response but to ping service from App1 to see if App2 did its job.

For how to implement WCF service see, for example: http://wcftutorial.net/WCF-Getting-Started.aspx

Basically:

on one side instanciate a "server"

UIIServiceHost = new ServiceHost(typeof(UIInterop));
UIIServiceHost.Open();

where UIInterop is a class implementaing IUIInterop that is a Service Contract

using System.ServiceModel;

[ServiceContract]
public interface IUIInterop {
    [OperationContract]
    void SetControlValue (UIControl c);
}

[DataContract]
public class UIControl {        
    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public String Value { get; set; }
}

Generate a proxy class => UIInteropClient

on the other side implement a client using the proxy class

using ( UIInteropClient proxy = new UIInteropClient("nameDependingOfAppConfig") ) {
    proxy.SetControlValue(new UIControl {});
}

===== EDIT =====

the name of classes and interfaces only reflect my lack of imagination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM