0

I run Firefox (default browser) from C# with the code:

System.Diagnostics.Process.Start(browser.Document.Url.ToString());

I want Firefox to run in the background, because every time is open a new tab, the Windows is focusing on the Firefox, and is annoying.

How can I control Firefox tabs, close them after a time ?

Aliostad
  • 80,612
  • 21
  • 160
  • 208
user689792
  • 291
  • 2
  • 4
  • 5

4 Answers4

2

You can use a ProcessStartInfo to tell it to run hidden or minimized or whatever. Not sure how to programmatically manipulate FireFox but I'm sure there's an API.

    var psi = new System.Diagnostics.ProcessStartInfo();
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    psi.FileName = browser.Document.Url.ToString();
    var proc = System.Diagnostics.Process.Start(psi);

    //after a while...
    proc.Kill();
Daniel Ives
  • 697
  • 5
  • 5
2

Technically, you are not starting Firefox, you are executing a url. I'm not sure exactly what Windows does, but in effect, that url is opened in the system's default browser, be it IE, FF or some other thing that might not even support tabs, so finding and killing Firefox is not really a solution if the url is opened in Opera.

Moreover, the Process.Start method returns null if no process is actually started by the call, so if Firefox is already running and just displays an additional tab, you will get a null as the result of the call.

So, I'm pretty sure this is impossible to do in a broad sence (any browser), and, unless Firefox has some sort of API for client-side management, not possible for that scenario either.

BTW, on my system (IE is the default browser), the WindowStyle property is not working as expected, as IE pops up to the front.

SWeko
  • 30,434
  • 10
  • 71
  • 106
  • Nothing is impossible, just some things are very hard! – Daniel Ives Apr 05 '11 at 11:53
  • 2
    By "Impossible in general" I mean that an application cannot know ***all*** possible browsers that can be set as default handlers for an url, and that it cannot know what should be done to instruct that particular browser to close a window/tab/audio stream/whatever. It would be **very hard** to make it work most of the time (WinXP/Vista/7 with IE/FF/Opera/Safari/Chrome) but it would be impossible to make it work ***all*** of the time - including the future (Win 8, FF 5, etc) – SWeko Apr 05 '11 at 12:07
  • I agree SWeko. That would fall under the category of "hard enough to be considered impossible to all intents and purposes". – Daniel Ives Apr 05 '11 at 12:36
0

Rather than trimming the tabs, why not just kill the entire Firefox process and restart it periodically?

BigMomma
  • 338
  • 2
  • 14
  • 2
    I would be very unhappy if any other app randomly killed my Firefox along with all the tabs and flash games and what not open. Especially if I use Chrome as default browser, and the url opens there. – SWeko Apr 05 '11 at 09:20
0

You won't be able to do this. First of all, I'm pretty sure running Firefox in the background won't stop it gaining focus when a new tab is opened. Second, it is difficult to control firefox programmatically. The only way to do what you want is to use a plugin like MozRepl. You could also try using selenium or your own JavaScript to control the browser behaviour. I needed to be able to open and close tabs in a shell script without using selenium or MozRepl, check out my question From a shell script open a new tab in a specific instance of Firefox

Community
  • 1
  • 1
toc777
  • 2,607
  • 2
  • 26
  • 37