I am running Phantomjs.exe
in the background.
I would like to keep this process running all the time. When it's time to use this process (on button click) I would like to check if the process is running. If it is running I would like to use that process. If it is not running, I will start a new process.
I've found out how to check if the process is running and how to start a new process in that case. But I don't know how to connect C# variable with already running process? (so there is no need to start - stop process each time, which takes a lot of time)
My code:
public bool IsProcessOpen(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
return true;
}
}
return false;
}
private void button1_Click(object sender, EventArgs e)
{
PhantomJSOptions options = new PhantomJSOptions();
PhantomJSDriver driver = null;
bool phantomOpened = IsProcessOpen("Phantomjs");
if(!phantomOpened)
{
PhantomJSDriver driver = new PhantomJSDriver(options);
}
else //set PhantomJSDriver to running exe
{
}
}