SunilRai86
SunilRai86

Reputation: 1020

Why doesn't this code kill the process?

Process.Start(jrekeypath);

Thread.Sleep(5);

foreach (Process clsProcess in Process.GetProcesses())
{
    if (clsProcess.ProcessName.StartsWith("javaw"))
    {
        clsProcess.Kill();
    }
}

It doesn't kill the process. Is there any problem in this snippet?

Upvotes: 2

Views: 539

Answers (2)

Rup
Rup

Reputation: 34408

Ditto the 5ms answer above, but you could also try keeping the process handle around and re-using that, e.g.

var javawProcess = Process.Start(jrekeypath);
Thread.Sleep(5000);
javawProcess.Kill();

Upvotes: 10

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

5 ms is not an extremely long time. Could it be that the process has in fact not yet started when your code looks for it, but instead starts shortly afterwards?

Upvotes: 5

Related Questions