I'm working on an application that starts other processes (some console applications, etc.). Each process has a user interface. When each process is started (via process.Start()
), I want to move the process UI to some other location on the screen (also on the second monitor). I know how to do this later after a "sleep" or button click action. The problem is that I want the window to move right after the process starts. The window handle is still not "available" (value is 0).
I searched for a solution and came across the process.WaitForExit(100)
method and the process.Exited
event handler. After investigating these, I discovered that:
the
process.Exited
event is called when the process ends and not when the process "loads," andprocess.WaitForExit(100)
causes the program to "sleep" when it is invoked.
So, I need some architecture guidance. How can I solve my problem without "sleeping" (for example, via process.WaitForExit(100)
)? Should I consider an approach that involves one of the following techniques:
- Mutex,
- Multithreading, or
- Async process start?
Or, is process.WaitForExit(100)
really OK (not "dangerous") for a stable application (if I will run up to 15 processes)? Here is my code example:
private void startApplication(
int aApplicationId,
string aBrowserPath,
string aAppPath,
int aMid,
int aAppLeft,
int aAppTop,
int aAppWidth,
int aAppHeight) // Try to start application process
{
Process process = new Process();
try
{
process.StartInfo.FileName = aAppPath;
//process.EnableRaisingEvents = true;
//process.Exited += new EventHandler(myProcess_Exited);
//process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit(100);
mProcessMap.Add(aApplicationId, process); // Add to process map
// Move process window to right position
IntPtr windowHandle = process.MainWindowHandle;
this.moveAppToRightPosition(windowHandle, aMid, aAppLeft, aAppTop,
aAppWidth, aAppHeight);
}
catch (Exception exc)
{
Console.WriteLine("ERROR! Process start exception!");
}
}