Dirty fix for closing browser down with no message
Posted: Thu May 08, 2014 9:33 am
Often when CefRuntime.Shutdown(); was called in Program.Main() during closing the browser I was getting an error message after viewing YouTube.com or because I tried to close the browser when a page was still loading and what I found out was some new tab-pages with an embeded browser were starting two new processes per tab.
I keep trying and just could not fix it no matter how I tried but i did come up with a dirty soloution that works
In program.main I save the current processID and then just before opening a new browser tab I record all the processId's that share the name with the current program using the code below
Then after the new tab opens I start a new thread, send it to sleep for a second and then get any new processID's for the current program using this code.
Now when I close tabs I wait a second to see if the tab killed it's processes and if not then I call a function to kill them and during unload/shutdown I just kill any tab processes that are not dead plus the main program thread just after "CefRuntime.Shutdown();" is called.
Yes I know it's dirty but it did the trick after spending a day looking for a clean solution.
I keep trying and just could not fix it no matter how I tried but i did come up with a dirty soloution that works
In program.main I save the current processID and then just before opening a new browser tab I record all the processId's that share the name with the current program using the code below
- Code: Select all
private static string OldProcessIDs = "";
public static string SaveCurrentProcessIDs()
{
Process PC = Process.GetCurrentProcess();
OldProcessIDs ="," + PC.Id;
foreach (System.Diagnostics.Process P in System.Diagnostics.Process.GetProcesses())
{
if (P.ProcessName == PC.ProcessName && P.Id != WebBrowser.MainProcessID)
{
OldProcessIDs +="," + P.Id.ToString();
}
}
return OldProcessIDs;
}
Then after the new tab opens I start a new thread, send it to sleep for a second and then get any new processID's for the current program using this code.
- Code: Select all
private void GetMyProcessID()
{//if SingleProcess = true then the processID will be zero
Thread.Sleep(1000);
this.ProcessID1 = Helper.GetNewBrowserProcessID();
Thread.Sleep(1000);
this.ProcessID2 = Helper.GetNewBrowserProcessID();//Don't ask me why but sometimes a second process is create
}
public static int GetNewBrowserProcessID()
{
Process PC = Process.GetCurrentProcess();
foreach (System.Diagnostics.Process P in System.Diagnostics.Process.GetProcesses())
{
if (P.ProcessName == PC.ProcessName && P.Id != WebBrowser.MainProcessID)
{
if (OldProcessIDs.IndexOf("," + P.Id) == -1)
{
OldProcessIDs += "," + P.Id.ToString();
return P.Id;
}
}
}
return 0;
}
Now when I close tabs I wait a second to see if the tab killed it's processes and if not then I call a function to kill them and during unload/shutdown I just kill any tab processes that are not dead plus the main program thread just after "CefRuntime.Shutdown();" is called.
- Code: Select all
#if !DEBUG
Helper.KillMyProcess(WebBrowser.MainProcessID);//On the release version also kill the main thread to stop shutdown errors
#endif
Yes I know it's dirty but it did the trick after spending a day looking for a clean solution.