C# - Open an app as second process in the Task Manager - c#

I want to open an different app and to appears like it's an process of the app itself and don't appear as a different one in the task manager processes list.
Something like:
Can Process.Start() do it?
Solved!
Well it seems that always when an app starts a process, the main app it'll be its parent. I didn't knew that :)

Using Process.Start with a ProcessStartInfo and ensuring UseShellExecute is false should probably do it. However, the process being launched may do something that breaks this behavior. It could, for example, just be a stub launcher that starts another process then quits.

Related

Detect a process exit in C#

I've an application which does
Process.Start()
to start another application 'firefox.exe'. I want to wait till that application ends (process dies) and continue my execution (example: Show a messagebox). There may be multiple instances of the application 'firefox.exe' running at the same time.
I have try WaitForExit and HasExited, but it return true right after firefox process start, so that the Messagebox show immediately.
How can i Show the messagebox in this situtation.
Update 1:
I tried this:
Process browser = new Process();
browser= Process.Start(#"dist\bin\firefox.exe");
browser.StartInfo.UseShellExecute = false;
browser.WaitForExit();
MessageBox.Show("AAAA!!!");
and HasExited similarly with EventRaising.
Update 2:
I have tried with many Simple Program like Notepad.exe, cmd.exe... All of them worked well. I think that Firefox call another process before running the main process and closing the original process. How can i bypass this problem.
If you can be certain that no firefox instance is already running you can simply use Process.GetProcessesByName("firefox"); and use WaitForExit on these processes.
If you want to handle multiple concurrent firefox instances you have a bit of a problem since firefox manages multiple processes in its own special ways. When you start firefox it might just ask the existing firefox process to create a new window and then quit. So you would need to detect new windows and monitor when this window is closed. I do not think there is any way to accomplish what you want simply by waiting on processes, at least not without cooperation by the program.
In the end I would try to do whatever you are trying to accomplish some other way. Perhaps you can host a webcontrol inside the application? Or perhaps embed a copy of Firefox Portable or some other stand alone browser in your application and start that instead?

Windows process abandon parent. How to get hold of them?

I wrote a program launching and monitoring and generally manage other programs. In the general case I prepare a System.Diagnostics.ProcessStartInfo and invoke Process.Start. Later I start one thread per program that's checking periodically the resulting Process object.
For testing propuses I configured my programm on my Win 7 machine to launch and monitor some typical Windows tools: Notepad, Paint and the Calculator. That all worked fine.
On a Windows 10 machine however the calculator behaves differently. The process started by Process.Start immediately terminates, and there is no calc.exe child to my launcher, but a win32calc.exe child to explorer.exe. child to a non existing process, the process my launcher created.
Why is that? How do I get a handle to that? What would be a strategy to monitor programs started by my launcher, even if they choose to abandon their parent?
A sort-of answer:
calc.exe launches the actuall process and then kills itself. Process groups could handle this: I could create a process group and monitor that.
However this was only a test and i monitor only applications that don't do that, I won't do that.

is Process.Start() synchronous?

I have a code like this:
ProcessStartInfo psi= new ProcessStartInfo(...);
Process process = Process.Start(psi);
Application.Current.Shutdown();
even so the process have the process information of the application (i have logs) in rare cases on the production computer the process is not opened at all.
As I now Process.Start() is synchronous and if it returns a value there must be a running process.
Another information that I have is that the genuine process is also the shell process.
Does anyone have an idea what is the problem?
Process is IO artifact, so there are always some delays, between you start it and it actually opened.
This delay, naturally, depends on concrete machine, where you run your code.
So, like a solution you can
or sleep main thread untill the p process opened, for some amount of time
or close the main thread, only when from (say) some timer you are able to find required p process in the list of already run OS processes.
The second, I think, is a better solution.

How to stop process in C#, knowing its filename?

I have a programm that runs another one (lets call the first app Stater and the second app - Worker).
I use
process.start();
process.waiForExit();
process.Close();
in Starter.
But if Starter is forced to close while waiting for Worker (for some extern reason) Worker will be still in processes, blocking files, eating memory etc.
So, I want to check if Worker is already running before I will try to start it.
I've tried Process.GetProcessesByName("worker.exe") but no luck (even if I can see Worker in Task Manager).
I've seen some topics here about checking every process in memory for its modules, but still I already know the running file I hope to avoid such solution.
Any advices?
The reason you cannot find it is because you're using .exe. If the executable shows up as worker.exe in TaskManager, just call:
Process[] workers = Process.GetProcessesByName("worker")
foreach (Process worker in workers)
{
worker.Kill();
worker.WaitForExit();
worker.Dispose();
}
At the time of starting Worker process, save its ID in your application's configuration/setting file, in this way when you will launch your Starter process, it will first load that ID from settings file and will check if that process is currently running or not. If you want to immediately close Worker process, you can call process.Kill() on it.
This is much easier...
System.Diagnostics.Process.Start("cmd.exe","/c taskkill /IM notepad.exe");
This code will close the Notepad (if it is running). Type the program name you want to close with it's extension (.exe).
Some applications cannot be stopped without forcing.
Use /F after taskkill to force the action.
If you want to close the process tree use /T after program name.
System.Diagnostics.Process.Start("cmd.exe","/c taskkill /F /IM notepad.exe /T");
When you call GetProcessesByName("worker") you don't specify exe extension as explained in MSDN
And if you wish to keep a global variable with the process object that you have started you could simply use the process.Kill();
If you only need to detect that "worker" is running, a technically much superior solution is have it lock a global mutex for the duration of its lifetime. Any process that knows the mutex name (which is under your control) can then see if the mutex is locked (if it is, worker is running).
However this is not easy to implement correctly as there are many little details you want to get just right; even then, there might be a race condition of "starter" and "worker" are actually launched simultaneously etc (of course this problem, and many others, also apply to all other solutions so it cannot be considered a drawback).

How can I recover a reference to a process that has been relaunched?

Using Process.Start, I am starting various IE8 and Firefox (3.5) processes which I keep a Process instance for.
A little while later in the application, I'll use the Process instances' MainWindowHandle property to target the window for use with some platform API functions via P/Invoke.
However, both IE8 and Firefox will kill the second process I start, then restart it using the first. This means that my Process instance now refers to a closed process, and so HasExited is true and MainWindowHandle is equal to IntPtr.Zero.
Here's an example of what happens (I'm using IE8 for this example):
Process.Start is called with "iexplore.exe"
Process starts and continues running
Process.Start is called again with "iexplore.exe"
First process continues running, but the second is killed immediately
Another iexplore process is started (presumably by the first iexplore process).
During this time, the user sees the second IE window only after the second process is killed and restarted.
I understand why these browsers behave this way, but it does create a problem for me if I want to control the created process later on.
Has anyone come across this problem before? How would you recommend getting a reference to the process back? I thought about using GetProcessesByName and iterating through their window titles, but unfortunately the titles may be the same as the first process launched.
Note: IE8 was the first version of IE to use process separation and so only IE8 behaves this way, I don't have this problem with IE7, unfortuantely I need IE8 support as well.
I have used the Running Object Table (ROT) in the past to find a specific process and control it.
http://www.codeproject.com/KB/COM/ROTStuff.aspx

Categories

Resources