Process

R

Ruslan Shlain

I am writing a Windows service. I have a timer that is set to 5 seconds. On
the elapsed event i stop my timer and i start a process that triggers an exe
app. It all works fine until I need to see when the exe finished its work. I
tried to use the following code to capture the event.
Any code samples or advice would be great.

This code is in Class1
static void Main(string[] args)

{

Class2 prc = new Class2();

prc.StartPrc();

//prc.StopPrc();

Console.ReadLine();

}


This code is in Class2.
public Class2()
{

prcImport = new Process();

prcImport.Exited += new EventHandler(Exited);

prcImport.StartInfo = new
ProcessStartInfo(@"C:\MyProjects\ImportWindowsService\ImportWindowsService\b
in\Debug\ImportWindowsService.exe");


}

public void StartPrc()

{

prcImport.Start();

}

private static void Exited(object sender, EventArgs e)

{

Console.WriteLine("{0}", sender.ToString());

}
 
G

Guest

Ruslan,

I'm not entirely sure because I don't have your code, but I think the thread that is waiting for the process to exit is blocked because of your call to Console.ReadLine. I got this to work by creating a seperate listener thread and blocking on Process.WaitForExit. Here is my sample code:

using System;
using System.Diagnostics;

public class blah
{
public static void Main()
{
System.Threading.Thread t1 = new System.Threading.Thread(blah.ThreadProc);
t1.Start();
while(!blah.done)
{

}
}

public static void ThreadProc()
{
Process proc = new Process();
proc.StartInfo = new ProcessStartInfo(@"C:\windows\notepad.exe");
proc.Start();
proc.WaitForExit(); // Block this thread until the application shuts done
done = true;
}

public static bool done = false;
}

Hope that helps

Jackson Davis [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights
Samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 

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

Top