Question

I have a c# multi thread application which launches an external exe file. In my launch code this launch thread is calling two times, some times the two instances of external exe file is getting launched. To get rid of this applied lock mechanism in launch method also adding time delay in launch. Like giving time delay of 5 milliseconds in a while loop, with a top level cut off time. And if the process found break the loop. And increment the top level cut off. And if top level cut off reaches 5 seconds and still the exe not found then break the loop. Below is my code.

int count = 1000;
lock (locker)
                {
 Process[] pname = Process.GetProcessesByName("MyApp");
if (pname.Length == 0)
{
----
 process = new System.Diagnostics.Process();
                        process.StartInfo = psi;
process.Start(); 

            while (true)
            {
                //find TG proceess
                pname = Process.GetProcessesByName("MyApp");

                //if found exit from loop
                if (pname.Length >= 1)
                {
                    Console.WriteLine("Found");
                    break;
                }
                else
                {
                    //give 5 sec delay
                    System.Threading.Thread.Sleep(10);
                    count = count + 10;

                    if (count >=5000)
                    {
                        Console.WriteLine("Not Found");
                        break;
                    }
                }  
            }
}
}

Is this code for time delay will work. Or am i doing something wrong.

Was it helpful?

Solution

I must say that understanding what you are exactly trying to achieve is a bit difficult.

The only way of finding out whether it will work is by running it and testing it against a set of test cases. Just thinking or asking someone that if it will work as expected is not the right approach.

If it meets your use case then only other consideration IMO should be the performance. If you are happy with the performance (and if it is as per your use case) then I think it should be good.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top