0

I ran out of ideas and couldn't find any reference about it so here I go... I need to keep a secondary application wich is not related to my c# project always running in background. So if this secondary application crashes or someone else close it manually it will automatically re launch again via my c# app.

I have no clue of how to accomplish this one, I mean checking if this application closes by something external to my c# app.

Any help would be greatly appreciated. Thanks in advance.

MikVal
  • 3
  • 2
  • What if, the user kills your C# application first and then closes the other application – Ramesh May 18 '12 at 07:20
  • @Ramesh if the user close my c# app then the other application won't re launch again. This specific behaviour can only occur when my application is running. I really need some code lines examples for this. – MikVal May 18 '12 at 07:34

5 Answers5

2

The below code is in C# and it is inside a WinForm.

    private void Form1_Load(object sender, EventArgs e)
    {
        Process p = Process.GetProcessesByName("Notepad")[0];
        p.EnableRaisingEvents = true;
        p.Exited += new EventHandler(p_Exited);
    }

    void p_Exited(object sender, EventArgs e)
    {
        MessageBox.Show("Exit");
    }

It looks for a Process with Name Notepad & retrieved the first instance of it. It sets EnableRaisingEvents to true on it and hooks to the Exited event. Whenever notepad is closed it would display an alert.

Based on this logic, you can build your app.

Ramesh
  • 13,043
  • 3
  • 52
  • 88
  • @Ramesh I have one more question if you're so kind please, the code you provided above is working perfectly but if the secondary application closes for a second time then it won't work. What should I add then to make it work this way? – MikVal May 18 '12 at 08:31
  • Either, you can create a new process in your handler and again attach its corresponding Exited event or, start a timer which will keep looking for the process untill it starts and once found, that timer should be disabled. – Ramesh May 18 '12 at 08:55
1

As a solution you can use Windows service which invokes your always running application .
You can make that service catch error return codes from the app and restart it depending on errors.

Damith
  • 62,401
  • 13
  • 102
  • 153
1

you can keep checking for a process if it is running or not using process class in vb.net

For Each p As Process In Process.GetProcessesByName("communicator") ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_NORMAL) Next p

if the process you want not inthe list you may launch it again. Ashish kumar

1

Simplest way is to run a timer and in the tick event, use-

if (Process.GetProcessesByName("communicator").Count() == 0)
{
   Process.Start("communicator.exe");
}
Kamrul
  • 324
  • 2
  • 8
  • I thought so at first, but perhaps doesn't it consume too much resources since it's always checking for an instance running when the timer ends? – MikVal May 18 '12 at 07:59
0

You can use FileSystemWatcher to keep a watch of the file modified by other application.

FileSystemWatcher has events like Changed, Created,Renamed, Deleted, which can be subscribed to keep track of a file changes.

Tilak
  • 30,108
  • 19
  • 83
  • 131