0

I am creating an application that when pressed will open a new tab in Chrome, and that tab will lead to a specific page that executes another function (I use threading because it needs time to execute the function on this page). Then closes the page. How would I be able to do so?

I would be thinking this code would work (google.com is obviously not the page).

var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
                Thread.Sleep(1000); //wait for the page to load and execute the function on the page
                proc.Kill(); //remove tab

But, I get the error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Cannot process request because the process has exited."

As well, the tab does not close.

Jack Rhan
  • 19
  • 6

4 Answers4

0

When executing chrome.exe, chrome itself checks whether another instance of chrome is already running (which is the case when you just want to add a new tab). So the newly executed chrome.exe just sends a bunch of information to the already running chrome.exe that the user tried to access the page given by cmdline-args. After that, your newly executed chrome.exe terminates.

The already running chrome.exe now creates a new child process for the new tab, which is completely different from the process just notifying it about the user interaction (i.e.: your chrome.exe instance).

A possible, though not completely error-free approach is scanning the list of open processes and scan for new chrome.exe processes after your call has finished.

But even killing that process will not close the tab. As you may have noticed in early days of chrome, when a website process crashes, the user gets notified about that issue and the page is reloaded (a new process gets started).

I guess you will have to either write a chrome extension for remote control or there may be an existing chrome api to achieve what you want to do.

Psi
  • 6,387
  • 3
  • 16
  • 26
0

I think it's because Process.Start return a 'null' instead of the process since chrome is already running.

that null in proc.kill() throws the exception.

Try looking at this question which did something similar- Open a Chrome Tab and Close it

Community
  • 1
  • 1
Shtut
  • 1,397
  • 2
  • 14
  • 28
0

You can use "Timer" tool from visual studio C# , it will do your job simply
look at this code :

 private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 100;
        }


int timerCounter=0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timerCounter += 100;

            if (timerCounter == 100)
            {
                var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
            }

            if (timerCounter == 5000) //5000 = 5  minutes , like waiting time for 5 mins  to execute the function on this page .
            {
                proc.Kill(); //remove tab
                timerCounter = 0;
                timer1.Stop();
            }

        }

 private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
Na'il
  • 29
  • 9
  • It won't allow you to use var proc at that location. Thus you cannot use "Proc.KillI();" – Jack Rhan Mar 04 '17 at 20:00
  • yes you right , var proc; if (timerCounter == 100) { proc = Process.Start("chrome.exe", "google.com"); //open chrome tab. } – Na'il Mar 04 '17 at 22:16
0

Since you have stated there may or may not already be an instance of Chrome running, this may yeild the results you're looking for:

// Open chrome tab.
var proc = Process.Start("chrome.exe", "google.com");

// Wait for the page to load and execute the function on the page.
Thread.Sleep(1000); 

// Check for valid running new instance of Chrome.
if (!proc.HasExited)
{
    // Kill new Chrome proc.
    proc.Kill();
}
else
{
    // Find already running proc of Chrome.
    var alreadyRunningProc = Process.GetProcessesByName("chrome.exe");

    foreach (var chrome in alreadyRunningProc)
    {
        // Kill any already running Chrome procs.
        chrome.Kill();
    }
}
Chris Cruz
  • 1,829
  • 13
  • 15
  • Just caught my error. Please try again. I needed to change the if statement to check if it 'has exited' rather than just a null object. – Chris Cruz Mar 04 '17 at 19:50
  • Hmm, yeah I just researched this a bit further and It seems like you may not be able to target the closing of a specific tab solely through C#. – Chris Cruz Mar 04 '17 at 20:03