Pepa Gazdoš
Pepa Gazdoš

Reputation: 9

Process.Kill() is causing fatal error in Winform application

today I want to open application inside of form, but it cause error.

                if (File.Exists(ts3))
                {
                    Process p = Process.Start(ts3);
                    Thread.Sleep(2000);
                    SetParent(p.MainWindowHandle, panel1.Handle);
                }

                foreach (var process in Process.GetProcessesByName(ts3check))
                {
                    process.Kill();
                }
                Thread.Sleep(500);
                Process p = Process.Start(ts3);
                Thread.Sleep(2000);
                SetParent(p.MainWindowHandle, panel1.Handle);

It should check, if this app is already openned, if it is, kill it and open new one inside of my form. But when I try to open process in form, it will cause fatal error and makes my form stopped working. I'm looking at it for like half a hour and I can't see it.

EDIT: app works if app is not already openned, but if it is, it will execute this part of code and make the error

            foreach (var process in Process.GetProcessesByName(ts3check))
            {
                process.Kill();
            }
            Thread.Sleep(500);
            Process p = Process.Start(ts3);
            Thread.Sleep(2000);
            SetParent(p.MainWindowHandle, panel1.Handle);

Upvotes: 0

Views: 368

Answers (1)

vane
vane

Reputation: 2215

Try changing Thread.Sleep(2000) to p.WaitForInputIdle() and you might also want to take a look at Hosting EXE Applications in a WinForm project over at CodeProject; someone has created a control to do exactly what you're looking for.

Upvotes: 1

Related Questions