2

So I've done some googling and can't work out whether the following is possible.

I am writing a piece of software which gets data using an interface mechanism provided by a third party program. On start up my program tries to connect to the third party program and if that fails then my program does not start. It then polls the third party program (and a background thread) to determine if any data has changed.

My problem comes when the third party application is closed whilst my program is running. For various reasons I can not simply close my software and instead I show a message telling the user they need to restart the third party program before they can continue using parts of the functionality of my program. I do however pause the polling. When the third party program has been started again I need to re-connect to it and continue the polling, however I can not work out how to determine when the third party application has been re-started.

  1. Is it possible to listen for the program to start? [by name and not by process as the process name is too generic]
  2. Is there a better alternative?
C. Knight
  • 739
  • 2
  • 5
  • 20
  • How do you listen to it when you start your program? – ZivS Jan 04 '16 at 15:05
  • I don't - I simply try and connect and if I can't then I close my program down. I could of course continuously poll to try and connect but that then uses try{}catch{} as program flow which is a bad idea. – C. Knight Jan 04 '16 at 15:07
  • Either way after searching for processes with name X you can filter out the processes by some specific unique info of that third party. e.g. path , version, file description etc – ZivS Jan 04 '16 at 15:09
  • Thanks, but that still would require polling to determine that the program had started. I guess I was asking if there is a way to listen for the program start instead. Does windows itself raise an event that I could listen for, for instance? – C. Knight Jan 04 '16 at 15:12
  • 1
    WMI is very powerfull... http://stackoverflow.com/questions/848618/net-events-for-process-executable-start – ZivS Jan 04 '16 at 15:17
  • Polling should suffice for you here. You could always just do a Process.GetProcesses("name") to see if the process is running at all before you do the try/fail polling. Unless the 3rd party program has created a failproof way to tell you its "online" you dont have many options. – Wolf5 Jan 04 '16 at 15:23
  • Thanks @ZivS that looks promising, haven't come across it before - good to learn new stuff. – C. Knight Jan 04 '16 at 15:26
  • One thing worth to check if this program maybe uses some sort of IPC already. Windows applications often create named mutex to prevent from running second copy, etc. – Sinatr Jan 04 '16 at 15:59

1 Answers1

0

Is there a better alternative?

You are already showing a message telling the user they need to restart the third party program. In addition to (or instead of) that message you can show a button to actually start the third party program. Example of starting a third-party program and passing it command line arguments.

using System;
using System.Diagnostics;

....
Process.Start("IExplore.exe", "www.northwindtraders.com");

Or you can just tell the user to start the third-party program manually and press the "Retry" button in your program. This is similar to "Alternative 1" above, but you don't need to start the third-party program yourself.

rsteward
  • 1,146
  • 2
  • 8
  • 9