Writing a .exe to start a .exe

  • Thread starter Thread starter richwood
  • Start date Start date
R

richwood

Ok....Super newbie here in C#...

I am trying to write an application (myapp.exe) that will start an
application called abc.exe and if it is terminated, will restart it.
(or rerun myapp.exe) I have tried it in several scripting languages,
but they tend to use ntvdm.exe and peg the processor.

Sounds easy, I know...but I am not real "sharp" at this. (horrible pun
fully intended)
 
The following seems like it should work... you might want to add some exit
conditions; as long as the app loads OK, it just reloads it every time it
exits. It doesn't thrash the CPU or anything.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = new
System.Diagnostics.ProcessStartInfo("notepad.exe");
while(proc.Start()) {
proc.WaitForExit();
}

That do?

Marc
 
1. Start the abc.exe using the System.Diagnostic.Process
class
2. Set process' EnableRaisingEvents to *true*
3. Hook to Exited event in order to be notified when the process terminates.

Read carefully the docs regarding EnableRaisingEvents property and Exited
even. There is some important information such as that you cannot use
reference to terminated process to restart it.
 
Ok....Super newbie here in C#...

I am trying to write an application (myapp.exe) that will start an
application called abc.exe and if it is terminated, will restart it.
(or rerun myapp.exe) I have tried it in several scripting languages,
but they tend to use ntvdm.exe and peg the processor.

Sounds easy, I know...but I am not real "sharp" at this. (horrible pun
fully intended)

And what kind of application is abc.exe, are you sure it's a 32 bit
application? My guess is that it's a 16bit application that runs in the
ntvdm. This scenario is not supported though.

Willy.
 
Thank you everyone for all the timely help. I am struggling through a
bit of this and have actually gotten the application (abc.exe) to
launch from myapp.exe. Now I just have to figure out how to loop this
so that if abc.exe is terminated for some reason, that it will restart.
(any thoughts on that would be greatly appreciated)

To answer the question, abc.exe is a 32 bit java app. that runs in its
own process. The problem comes in that when one of our other java apps
is shut down, it takes out the abc.exe process.

Thanks again everyone for all the help.
 
Hi richwood,

There were couple of solutions in the post.
Don't any of them work for you?
 
Actually, yes....They did help quite a bit. I am to the point where I
can open abc.exe in its own process, but I trying to figure out how to
put a "Wait" in, so that when the process terminates, it will start up
again.
 
Check out the WaitForExit method in the Process class.. You can either wait
indefinitely - or wait a specific amount of time..

Then you'd obviously just put that inside a loop: start the app, wait to
exit, rinse and repeat

HTH
 
To start a process you use the Process class. You can also attach to already
running process:

while(true)
{
using(Process p = new Process(...))
{
p.Start()
p.WaitForExit()
}

}

When you start the process your method will block until the started process
exits. The you loop and start it again. You may also set some condition to
make the loop end.

If you don't want to block the main process you can use what I suggested

p = new Process(...)
p.EnableRaiseEvents = true;
p.Exited = new EventHandler(ProcessExited);
p.Start()

void ProcessExited(object sender, EventHandler e)
{
((Process)p).Dispose();
Process p = new Process(...)
p.EnableRaiseEvents = true;
p.Exited = new EventHandler(ProcessExited);
p.Start()

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top