开发者

Starting and stopping firefox from c#

开发者 https://www.devze.com 2022-12-21 03:37 出处:网络
When I start /Applications/Firefox.app/Contents/MacOS/firefox-bin on MacOSX using Process.Start() using Mono, the id of the process that gets returned does not match the process that firefox ends

When I start

/Applications/Firefox.app/Contents/MacOS/firefox-bin

on MacOSX using Process.Start() using Mono, the id of the process that gets returned does not match the process that firefox ends up running under.

It looks like firefox quickly decides to start another process, and kill the current one.

This makes it difficult to stop firefox, and to detect if it is still running. I've tried starting firefox using the -no-remote flag, to no avail.

Is there a way to start firefox in such a way that it doesn't do this "I'll quickly make a new process for you" dance?

The situation can somewhat be detected by making sure Firefox keeps on running for at leas开发者_高级运维t 3 seconds after its start, and when it does not, scan for other firefox processes. However, this technique is shaky at best, as on slow days it might take a bit more than 3 seconds, and then all tests depending on this behaviour fail.

It turns out, that this behaviour only happens when asking firefox to start a specific profile using -P MyProfile. (Which I need to do, as I need to start firefox with specific proxyserver settings) If I start firefox "normally" it does stick to its process.


When using mozilla programmatically / embedding in other apps you should use the provided components:

"The right way" as recommended by mozilla ...

https://developer.mozilla.org/en/XPCOM

"The easy way", general consensus from Mozilla dev guys ...

http://www.iol.ie/~locka/mozilla/control.htm

Need to "execute processes", simply instantiate an instance of the component and talk to that, you should then have everything you need.

However ...

If you really do insist on looking after the actual process for a firefox instance, you may want to call

Process.Start("...\firefox.exe");

Then try this (i believe this works on MAC OS too) ...

C#/mono: get list of child processes on Windows and Linux

The basic idea / thought pattern being that if it is in fact executing another process right away you should be able to find that as would be a "child" process instead of being just one from a list in the "task list" / process manager for the machine.


In case you are ok with the detection of Firefox process itself but don't know when you should start the detection, you can use the Process.Exited event. Don't forget to set EnableRaisingEvents first.


I suggest taking a different approach: list all running processes, find Firefox, get it's process ID and (optional) see if it is the one you started.

I'm not sure if there's a platform-independent way to accomplish this. On Linux, you can call "ps -ef" to list them, on Windows it's called "tasklist".


I don't have a Mac, so I can't be sure, but if it's anything like Linux the firefox 'binary' is actually a shell script which sets up a bunch of required environment variables and checks if firefox is already running (if it is, it will try and add a new tab to the existing instance, as far as I know).

You may be able to capture the new PID within the script, although if you update it do beware of it being overwritten during upgrades.

0

精彩评论

暂无评论...
验证码 换一张
取 消