Niels Bosma
Niels Bosma

Reputation: 11498

How to open browser and then kill it?

I'm implementing a OAUTH-type of authentication for a desktop application and in this process I need to open a webbrowser and then close it when token has been collected.

I've tried with this:

var p = Process.Start(new ProcessStartInfo(url));
...
p.Kill(); //this won't work as p is null = no new process started

According to the documentation:

If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, Start returns null to the calling procedure.

Indeed, the page is just opened as a new tab in Chrome (my default browser) process that I've already have opened.

Any ideas on how to open a new process of the default browser that I can kill?

Upvotes: 1

Views: 3426

Answers (2)

Sagiv b.g
Sagiv b.g

Reputation: 31024

I have written a small function to open a webrowser using SHDocVw.

you will need to add a reference to a COM component called Microsoft Internet Controls. you can see it here

it enables you to manipulate the window as you want. (the Task.Run is only to open it in a new thread so you can remove it if you want)

when you done you can call ie.Quit()

public void OpenKioskBrowser(string URL)
{
    Task.Run(() =>
    {
        SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
        ie.Navigate(URL);
        ie.ToolBar = 0;
        ie.AddressBar = false;
        ie.Width = 350;
        ie.Height = 200;
        ie.Visible = true;
    });
}

another example of usage (Console application):

static void Main(string[] args)
{
    SHDocVw.InternetExplorer browserObj = new SHDocVw.InternetExplorer();
    browserObj.Navigate("http://stackoverflow.com");
    browserObj.ToolBar = 1;
    browserObj.AddressBar = true;
    browserObj.FullScreen = true;
    browserObj.Visible = true;

    Thread.Sleep(5000);

    browserObj.Quit();
}

Upvotes: 2

Jannik
Jannik

Reputation: 2429

The main problem here is, that the second process will be opened, Chrome notices that one instance is already open, so it kills your process and adds it to you already opened process. When you try to kill the process, its already closed. But I think you already sorted that out.

What I found is this question, it could help you, but I haven't tested it, it might be using the same process aswell, but anyway, try it out:

https://superuser.com/questions/213460/how-can-you-configure-chrome-to-open-new-browser-instances-in-new-windows-rather

Upvotes: 4

Related Questions