In my application, which using another application (run in tray) to print receipts I need to do those three things:
Open process when on mainApplication startup
Close process when mainApplication closing or changing any information about printer
Keep process alive, if it get any error
First point is quiet easy, I just simply
Process.Start("_ReceiptPrinter.exe");
And process working ;)
But now, the two other issues:
Closing process. I've tried this code:
Process[] allProcs = Process.GetProcesses();
foreach (Process proc in allProcs)
{
ProcessThreadCollection myThreads = proc.Threads;
if (proc.ProcessName == "_ReceiptPrinter")
{
proc.Close();
}
}
Unfortunately, I can still see icon in tray, and process is still running.
Keep process alive. My main application is in WPF, that one from tray is written on WinForms. Maybe there is any way to handle ANY WinForm application exit event (well, any, but not this one, which just simply close it from another application), and reopen it?
proc.Close() asks it to close but there is no guarantee. Use:
proc.Kill();
The reason you still see a tray icon is that the icons are cached by an external process (windows explorer.)
The reason process.Close() does not close the application is because the application is not processing window messages (as this call simulates a WM_CLOSE request, per classic Windows API.)
The proper way to close the application is process.Close, not process.Kill(), further, as part of app/window close you need to unregister any tray icons you've registered with the system. This way any normal closure of your application will properly clean-up the tray.
Further, you can use a "critical finalizer" which would be guaranteed to run before application exit, except in total catastrophe scenarios.
Related
So I have a hidden console application called Hidden.exe running.
Another application Call Killer.exe will find the process Hidden.exe or its PID, and Kills the process.
How do i programmatically capture a kill command or a terminate from Task Manager? A user can browse through the process list and 'End Task' on Hidden.exe and I want to be able to capture this event and do some cleanup before it exits.
How can i do this? I have searched around, and explored alternatives from
.NET console application exit event
Send WM_CLOSE message to a process with no window
Can I send a ctrl-C (SIGINT) to an application on Windows?
etc....
But they all dont work or only work in some cases, my case is for a hidden console application and needs to somehow capture a Kill on it. None of the above solution seem to have a 'correct' solution.
There is no such answer. A kill will always work and will fire no event. This is due to security concern to prevent virus and/or malware code.
I've since found another way.
I am trying to close a specific singleton handle associated with a process. The windows application "Process Explorer" allows you to do this but you need to select the process, then right click on the handle and select close. I am after a solution that will allow me to auto close the specific handle when the application is running.
I know I can get all processes and even kill a process by simply doing:
foreach (var p in Process.GetProcessesByName("your.exe"))
{
p.Kill();
}
But how would I go about closing a handle attached the process and not the process itself?
I ended up using NtDuplicateObject as Michael suggested in the comments. It was a bit of a pain to implement but everything works as expected - thanks Michael!
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?
I have a question, I created an application on visual studio using C# and Window Forms, but every time it runs, if the application close (either manually by the person or when it reach the end) it continue running on the process of the computer, so if I open and close the application 3 times there will be 3 processes with the same name activated. How can I prevent that from happening? So far the only way for me to close it is going to Window Task Manager and closing it manually, which is a pain...
Any ideas?
if you want to kill all processes and exit from application then first you need to kill threads in background
Application.ExitThread();
and then exit from application
Environment.Exit();
You can use Saif's answer which forces background threads to abort, but I'd recommend you manage your threads better. If you have threads that run for an extended period of time, you should have a flag (a boolean that can be accessed from anywhere in your code) that tells the threads they should stop running. This is a safer method than
Application.ExitThread();
because it allows you to flush and close streams, disconnect your socket connections or tidy up whatever you're doing in your threads.
Well, based on this problem you can check if your application is running on your application startup. That way you only start the process if it is not running:
I´m going to check for example notepad:
Process[] pname = Process.GetProcessesByName("notepad");
if (pname.Length == 0)
{
//The application is not running. Start your process here
}
else
{
//Your application is running. Do nothing
}
i was wrote some codes, when my apps still run, it will close another (example notepad) even the notepad is reopen it will close again, i've try some, but it will close when my apps startup , when my apps is running, and i open notepad, notepad wont close. here
foreach (Process Proc in Process.GetProcesses())
if (Proc.ProcessName.Equals("notepad"))
Proc.Kill();
Your code kills processes that are running at the time you code executes. Once your code has finished executing it no longer exerts any influence. It won't kill processes that are started after your code has finished executing.
Probably you need to detect when the target process starts, and then kill it. You can do that by polling which is rather inelegant. To avoid polling you need WMI. There are many examples of how to do this. For instance: How to detect a process start & end using c# in windows?