I want to make a program that will check to see if a process is running. And if it is not running, the program should start it back up.

Basically i made a new process. This is what is executed when the button is clicked.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "program.exe";
            proc.Start();

I also have it enabling a timer.

timer1.Enabled = "true";

This is the part i'm having trouble with. I'm trying to think of a way to have the timer check to see if the program is running, and if its not to start it up.

Any suggestions on how to do this?

Thanks,

SiPex

Member Avatar for iamthwee

Try looking at the process id.

Try looking at the process id.

I dont understand.

Member Avatar for iamthwee
Private p As Process = Nothing
    
        Dim I As Integer = 0

        While I <> 1 'I.e a condition that will never happen
            System.Threading.Thread.Sleep(1000) ' wait 1000ms
            If p Is Nothing OrElse p.HasExited Then
                p = New Process()
                p.StartInfo.FileName = "c:\virus.exe"
                p.Start()
            Else
                ' This process is already running so
                ' bring is to the front/maximize it
                ' instead of opening another one.
                AppActivate(p.Id)            
           End If
        End While

The following example would keep opening the program if it was closed down. Instead of using the timer class I used system.threading.sleep . All I did was put the sleep function in an infinite while loop. It's vb.net but should be easy enough to follow.

[btw, in the above example the only way to kill the app is to shut it down from task manager. :)]

Not really the code i used, but the concept helped me thanks!

Member Avatar for iamthwee

Glad it helped :) Maybe you could post your solution for the benefit of the daniweb community, and anyone viewing this thread a couple of months down the line.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.