Process.Start("IEXPLORE.EXE") immediately fires the Exited event after launch.. why? - c#

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.
this is the sample code
Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");
if (objProcess != null)
{
objProcess.EnableRaisingEvents = true;
objProcess.Exited += new EventHandler(myProcess_Exited);
}
public static void myProcess_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("You exited");
}
But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.
this only gives problem launching IE 8. Can someone clarify me what is the problem??
UPDATE
Most friends replied my post and saying why you can't just use an URL? why stick with IE?
here the reason
the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.
Thanks

Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.
Possible solution:
try starting the process with "-nomerge" command line option:
Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Interesting observation: objProcess.ExitCode (for IE8 at least) will be equal to 0 if exited passing control to another instance, and 1 if it was actually closed by user.

If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architecture of Internet Explorer 8 has the parent launch child broker process and exit immediately.
But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try
System.Diagnostics.Process.Start("http://google.com");
directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.

Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a fork on Unix?

Related

Process.Exited raising immediately after Process.Start [duplicate]

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.
this is the sample code
Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");
if (objProcess != null)
{
objProcess.EnableRaisingEvents = true;
objProcess.Exited += new EventHandler(myProcess_Exited);
}
public static void myProcess_Exited(object sender, System.EventArgs e)
{
MessageBox.Show("You exited");
}
But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.
this only gives problem launching IE 8. Can someone clarify me what is the problem??
UPDATE
Most friends replied my post and saying why you can't just use an URL? why stick with IE?
here the reason
the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.
Thanks
Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.
Possible solution:
try starting the process with "-nomerge" command line option:
Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Interesting observation: objProcess.ExitCode (for IE8 at least) will be equal to 0 if exited passing control to another instance, and 1 if it was actually closed by user.
If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architecture of Internet Explorer 8 has the parent launch child broker process and exit immediately.
But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try
System.Diagnostics.Process.Start("http://google.com");
directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.
Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a fork on Unix?

Process.WaitForExit() not waiting for multiple Chrome instances

I'm trying to start Chrome from WPF application using .NET framework 4.0 with code similar to the following:
var arg = string.Format("--app=\"{0}\" --window-size=1024,1000", "http://bing.com");
this._process = Process.Start("chrome.exe", arg);
this._process.WaitForExit();
// Perform relevant operations once the process completes/ exit
I'm opening chrome in app mode.
Case 1: When I have a no additional Chrome instance/window opened, WaitForExit blocks the control till the Chrome window created through code is closed - This is what I'm looking for.
Case 2: If I have a Chrome instance running already. Then it does not wait for the chrome instance created through code to exit and moves on to the next line. I want to have similar experience as in case 1, that is the control should be blocked until the user closes the chrome instance.
Is there anything extra I need to do get this working when I have multiple instances of Chrome already opened?
You can use Process.GetProcessesByName to get all opened chrome process,and invoke WaitForExit for each process
Process.Start(#"%AppData%\..\Local\Google\Chrome\Application\chrome.exe",
"http:\\www.YourUrl.com");
You can also try with dis
Process.Start("chrome", #"http://www.stackoverflow.net/");

issue with starting and a killing a process in windows 7

i am trying to start a browser instance as a process from a c# code. then i want to kill the same instance of the browser. I tried finding the same instance with process id . But the process ids are different in task manager and the initial id which i got when i started the process.
what's the solution? why is this happening? Development enviorment is windows 7.
int ID= 0;
void Start()
{
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe");
startInfo.Arguments = "http://www.google.com";
Process ieProcess = Process.Start(startInfo);
ID= ieProcess.Id;
}
void Stop()
{
foreach (Process p in System.Diagnostics.Process.GetProcessesByName("iexplore"))
{
if ((p.Id == ID))
{
p.Kill();
}
}
This code will not work if IE is already launched. Close all IE browsers and then try to run the code. If it works then you may have to look for solution suggested in following link
similar post-
Process.kill() denied in Windows 7 32bits even with Administrator privileges
Why don't you add your code to the question? It'll make life easy for the people who are interested in helping you. If you get different PIDs, most probably there's something wrong with your code! (I'm just guessing without seeing what you have tried.)
Have a look at these questions as well.
1) Getting PID of process started by Process.start()
2) Programmatically kill a process in vista/windows 7 in C#
3) Process.kill() denied in Windows 7 32bits even with Administrator privileges
Adding the code makes it much easier to understand what the problem is and here's your problem.
IE creates more than one process for one instance of the program. (more details about it) That's why you get different PIDs (for the different processes).
What your code does is killing only one process of it (by the usage of if condition in the Stop() method!). So the remaining process may generate InvalidOperationException when you try to execute Start() again(starting the same process)!
So what your code should do is kill all the active iexplore processes. This can be done by simply removing the if condition of Stop() method.
foreach(Process p in Process.GetProcessesByName("iexplore"))
{
p.Kill();
}
Let me know whether this worked.
I have a similar issue, only I dont want to kill the IE process that I started, I want to bring it into focus.
I have one app that starts 5 IE windows.(not tabs, but unique windows)
I store the PIDs that I start each of the IE windows with.
At particular times, I want to be able to:
select a PID,
find the IE window related to that PID
bring it into focus (minimizing the others)
This worked using XP and IE6 (required for the environment)
Now when I am using Win 7 and IE 8, the PID that I stored is not found,
and thus I no longer have the ability to change the window in focus.

Process.Kill() issue

I have the following lines of code:
Process aProcess = Process.Start("ieexplorer", aDummyHTMLFilePath);
//I do nothing in between
aProcess.Kill();
This runs smooth if there are NO other IE windows open.
But if there is a window open, I get a System.InvalidOperationException on aProcess.Kill(); saying :
Cannot process request because the process has exited.
Also, I notice that in this case, aProcess.HasExited is true right after line 1 in the code above.
How can I smoothly close IE, even if there are other IE windows open?
When you start a new instance of Internet Explorer like you do it will try to see if Internet Explorer is already running. If that is true the URL is opened in the already running instance and the new instance exits immediately. This means that when you try to kill the process you started it has already exited voluntarily. However, you will see a new browser window or tab on your screen but that is being hosted by the existing Internet Explorer process.
I didnt get the same issues as you
I found that the following worked if the process was still running or not
Process p = Process.Start("notepad.exe", "");
Thread.Sleep(5000);
if (!p.HasExited) p.Kill();
It only complained IF the window had been closed. Hence the check

Prevent user from closing Firefox

I absolutely need a user to log out of a website which she/he uses to access our database. If one doesn't log out, and simply closes the browser, the system locks the username for an hour. I did not implement it, it's just the way it works.
I thought to write a simple C# program that would somehow detect whether the user logged out, and if not, prevent them from closing the browser.
1) Is there Firefox API, or any other way to read the website content in firefox.exe process?
2) When a user hits 'X' to close the browser, is it possible to abort the termination of firefox.exe process? (probably this is the deal-breaker question).
I would appreciate any hints. Thanks!
Every user should have a session, if you are using authentication. Sessions can be configured to expire. All you need to do is just to handle an appropriate session expire event.
Do not look for any hack to handle a browser exit event - it is a dangerous path:
Everybody would hate it, and there is no chance you can make it reliable, i.e. working in newer versions of browsers, supporting many browsers on different OS etc
This may work in Firefox Mozilla (sample), but may not work in IE or Chrome
Application can be killed by OS, so browser events will not get a chance to fire and your handling code will not work
You can't stop the user from closing firefox, because there a lot of ways it can be closed that can't be controlled by your code (e.g. killing the process from taskmon). However, you can detect the closure event in your code (window.onClose() event) and do the log out process.
However, in case of firefox (or any other browser for that matter), gets killed rather than being closed, window.onClose() will not work. So its better to handle the session in the server rather than depending on the client behavior.
What you can do is use javascript to detect the browser closing or leaving your site and in those instances fire a method or hit a webservice that logs them out. Very quick, very simple and will work across browsers.
You cannot prevent a user from closing a program with legitimate methods. This is for obvious security purposes.
I agree with oleksii. But you can make it less easy for users to close Firefox.
I'm currently writing an add-on (via the SDK) that listens to the "quit-application-requested" Observer Notification and sets aSubject.data to true preventing Firefox from quittng the application.
observe: function(aSubject, aTopic, aData) {
if (aTopic == "quit-application-requested") {
aSubject.QueryInterface(Ci.nsISupportsPRBool);
aSubject.data = (this.canQuit() == false);
}
}
But what I actually want is a certain window not being closed, as to not close the web application (a FileMaker database).
Therefore I've also tried disabling all commands, all keys and all menus (browser XUL), like so:
var allCommands = win.document.getElementsByTagName("command");
for (let i = 0; i < allCommands.length; i++) {
allCommands[i].setAttribute("disabled", "true");
}
var allKeys = win.document.getElementsByTagName("key");
for (let i = 0; i < allKeys.length; i++) {
allKeys[i].setAttribute("disabled", "true");
}
var allMenus = win.document.getElementsByTagName("menu");
for (let i = 0; i < allMenus.length; i++) {
allMenus[i].setAttribute("disabled", "true");
}
But users can still quit the application and thus close the window with the web application unless I prevent opening new Firefox windows.
So "it" works but I would like for them to still browse and just not close a specific tab or window in which the tab resides.
Without preventing opening of new windows it doesn't work because apparently when a user opens a new window then when users press Alt+F4 it would not be an application quit but a window close. Although I thought I had disabled all commands/keys/menus, still there exists some kind of close possibility.
Does anyone have any ideas on how to accomplish that without preventing the opening of new windows?

Categories

Resources