I am trying to start an external process and wait for it to be active before continuing the execution of my program. I am searching based upon process name, but I have a problem with my implementation if the process name is not what I expect.
//When Method1 is called, it will load the data and bring a pop up
//as adobe save dialog box (as a save dialog exe in the task bar)
Method1();
while (true)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains("AdobeARM"))
{
isOpened = true;
}
}
//Once the pop up from Method 1 comes i call other methods
if (isOpened)
{
Method2();
Method3();
Method4();
break;
}
}
This could cause an infinite loop if the process is never found! What is the best way to handle this or alternative to while loop?
Get rid of the while.
It’ll perform the check and then if the AdobeARM process is there it will run your methods and if not will skip them. Put everything right there after Method1 in a separate function and call that function from a System.Threading.Timer.
Note that depending on how it is used in your application you may want to add extra handling for threading issues.