0

I'm trying to test some things with PhantomJS (v 2.0) in C# and is not closing it properly, which causing that my machine uses the 100% of the CPU.

All the program is working fine, except the part to close it.

i tried with

driver.Dispose();
driver.Quit();
driver.Close();

and (all of them)

Enviroment.Exit(-1);
Enviroment.Exit(0);
Enviroment.Exit(1);
return;

but the process still in the task manager

i tried to close the console brutally

 Process.GetCurrentProcess().Kill();

but still not working.

there is the message that phantom shows to me in the console

[INFO  - 2015-10-19T09:43:58.542Z] GhostDriver - Main - running on port 62810
[INFO  - 2015-10-19T09:44:17.480Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true
,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"loc
alToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.1; WOW6
4) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1","webSecur
ityEnabled":true}
[INFO  - 2015-10-19T09:44:17.499Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - page.customHeaders:  - {}
[INFO  - 2015-10-19T09:44:17.507Z] Session [f7138220-7645-11e5-b667-c72dd4c4644c
] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.0.0
","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-7-32bit
","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databas
eEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"
browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":f
alse,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"prox
yType":"direct"}}
[INFO  - 2015-10-19T09:44:17.530Z] SessionManagerReqHand - _postNewSessionComman
d - New Session Created: f7138220-7645-11e5-b667-c72dd4c4644c
[INFO  - 2015-10-19T09:45:27.620Z] ShutdownReqHand - _handle - About to shutdown

in addition, i'm using selenium, but i don't think that there is an important information.

Any idea?

Thanks.

TiGreX
  • 1,602
  • 3
  • 26
  • 41
  • Unfortunately, this seems to be a common issue. See [this](https://github.com/detro/ghostdriver/issues/162) and [this](http://stackoverflow.com/questions/25110624/how-to-properly-stop-phantomjs-execution). – Anders Carstensen Oct 19 '15 at 10:03

3 Answers3

3

Here is how I was able to solve this using Selenium WebDriver and PhantomJS.

var service = PhantomJSDriverService.CreateDefaultService();
try
{
    driver = new PhantomJSDriver(service);
    driver.Navigate().GoToUrl("http://www.google.com");
}
catch (Exception ex)
{
    service.Dispose();
}
finally
{
    service.Dispose();
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
CowCaBob
  • 66
  • 1
  • 5
0

Possible Solution

I'm not going to mark it as valid because is temporal, but for now, the easiest/Fastest way to solve the problem is get the process and kill it manually.

Why is not the best solution, because is going to kill ALL PhantomJS process, in my case there is only one, then is valid, but is better than lock the server.

foreach (Process proc in Process.GetProcessesByName("PhantomJS"))
{
    proc.Kill();
}

if anyone knows a way to get a specific process (if there are more than one) will be welcome.

TiGreX
  • 1,602
  • 3
  • 26
  • 41
0

Something like this seems to work well in this scenario:

        //Close Driver
    driver.Close();

    // Get the current process.
    System.Diagnostics.Process myProcess = System.Diagnostics.Process.GetCurrentProcess(); 

    // Close process by sending a close message to its main window.
    myProcess.CloseMainWindow();

    // Free resources associated with process.
    myProcess.Close();

    Environment.Exit(0);
Pat
  • 580
  • 4
  • 14