2

C#WinForms: There are a couple of ways for two applications to talk together, I am not very knowledgeable in this area but things like MSMQ and Named Pipes comes to my mind but not sure what is the best. So here is the scenario, what do you think is the best approach:

Let's say I write a windows service that backs up some files to somewhere on occasion. User opens some my Application XYX and I want him to be notified that hey there is new backup file for you over there.

That's all. This was the scenario.

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • for your scenario, you can do it with database :) you can add record to db when you put backup, when XYX app open, you can check for backups from db and notify – Damith Jun 18 '12 at 04:44
  • yep, That's true..even simpler, writing a new line in a text file :) But what about other classy ways? something that makes boss impressed! like he says oh wow he has used XYZ to do that! – Bohn Jun 18 '12 at 04:47
  • Database would be additional overhead. You need to install some database to each client Machine where ever this application would be hosted. Whereas MSMQ and named pipes are native component of Windows. With Text file(and hence name pipes), the only thing that you would have to worry would be you would be playing with streams rather than objects ! – Anand Jun 18 '12 at 04:53
  • @Anand Database will be additional overhead if there is no Database involved with current applications – Damith Jun 18 '12 at 04:57
  • 1
    @BDotA Using a particular technology because it'll make the boss go "wow" is a very bad reason. You should be thinking about factors such as simplicity, reliability, maintainability. Requiring additional infrastructure (e.g. messaging system, a database server etc.) to be installed, configured, maintained, updated, etc. when writing a line to a simple text file would suffice is just going to cause problems either for you, or the person that has to maintain the system after you. – Iridium Jun 18 '12 at 04:57
  • We already have an Oracle and a HBase involved for working in this application tho. – Bohn Jun 18 '12 at 05:00

3 Answers3

1

Use MSMQ as it is very simple to implement and you could play with objects. Producer and Consumer then can interact with each other very simply.These two applications(Producer, COnsumer) can be on the same machine, across a network, or even on different machines that aren't always connected. MSMQ is considered failsafe in that it will retry sending a message if the first transmission fails. This allows you to be very confident that your application messages will arrive at their destination.

More Details

Anand
  • 14,545
  • 8
  • 32
  • 44
  • Thanks Anand, I had posted a MSMQ question before, was wondering if you half a new minutes could you please help me with that? http://stackoverflow.com/questions/11076790/the-bare-minimum-needed-to-write-a-msmq-sample-application – Bohn Jun 18 '12 at 04:53
  • 1
    check the above link, I have posted my answer in your question itself – Anand Jun 18 '12 at 05:05
1

We just used Named Pipes for a similar purpose on a recent project. Code turned out to be really simple. I can't claim credit for this particular code but here it is:

    /// <summary>
    /// This will attempt to open a service to listen for message requests. 
    /// If the service is already in use it means that another instance of this WPF application is running.
    /// </summary>
    /// <returns>false if the service is already in use by another WPF instance and cannot be opened;  true if the service sucessfully opened</returns>
    private bool TryOpeningTheMessageListener()
    {
        try
        {
            NetNamedPipeBinding b = new NetNamedPipeBinding();
            sh = new ServiceHost(this);
            sh.AddServiceEndpoint(typeof(IOpenForm), b, SERVICE_URI);
            sh.Open();

            return true;
        }
        catch (AddressAlreadyInUseException)
        {
            return false;
        }
    }

    private void SendExistingInstanceOpenMessage(int formInstanceId, int formTemplateId, bool isReadOnly, DateTime generatedDate, string hash)
    {
        try
        {
            NetNamedPipeBinding b = new NetNamedPipeBinding();
            var channel = ChannelFactory<IOpenForm>.CreateChannel(b, new EndpointAddress(SERVICE_URI));
            channel.OpenForm(formInstanceId, formTemplateId, isReadOnly, generatedDate, hash);
            (channel as IChannel).Close();
        }
        catch
        {
            MessageBox.Show("For some strange reason we couldnt talk to the open instance of the application");
        }
    }

In our OnStartup we just had

        if (TryOpeningTheMessageListener())
        {
            OpenForm(formInstanceId, formTemplate, isReadOnly, generatedDate, hash);
        }
        else
        {
            SendExistingInstanceOpenMessage(formInstanceId, formTemplate, isReadOnly, generatedDate, hash);
            Shutdown();
            return;
        }
Nogusta
  • 909
  • 5
  • 10
0

There are many ways to achieve what you have asked;

  1. As Damith said make an entry to database and read from it.
  2. Make an entry in a file and read from it.
  3. Use WCF - Windows Communication Foundation and set the configuration to use the MSMQ bindings. Read WCF & MSMQ article to get you started.
wonea
  • 4,783
  • 17
  • 86
  • 139
Sharkz
  • 458
  • 1
  • 9
  • 25