2

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?
Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
user1617141
  • 115
  • 1
  • 11
  • Why is your process dying in the first place? If you're getting an error and it's killing your process then you need to think about error handling. – Ian R. O'Brien May 22 '14 at 14:50
  • Any reason why you don't have just one application? It's possible to add tray functionality to a WPF app. I've used NotifyIcon referenced in this thread http://stackoverflow.com/questions/1472633/wpf-application-that-only-has-a-tray-icon – kenny May 22 '14 at 14:57
  • @kenny - reason is that, DLL's that I'm using in WPF doesn't work. After my receipt is printed I got stack overflow error. Work's on empty and my project. In WinForms everything work's without problem. – user1617141 May 22 '14 at 15:02
  • @IanO'Brien - in my app (wpf) i have method to catch unhandled exceptions. That keeps application open (I mean i have window with error message and button which allows user to send log to me). But not sure if there anything like that in WinForms. EDIT: In WPF I'm using Current.DispatcherUnhandledException += new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException); – user1617141 May 22 '14 at 15:04
  • Well, maybe this will help: System.Windows.Forms.Application.ThreadException – user1617141 May 22 '14 at 15:08

2 Answers2

3

proc.Close() asks it to close but there is no guarantee. Use:

proc.Kill();
Michael Kniffen
  • 346
  • 2
  • 7
  • I don't really understand what you mean by the third part. Are you trying to keep the Tray Based process alive if an error happens in the WPF app? – Michael Kniffen May 22 '14 at 14:47
  • Nope. I want to do something like: If tray (winforms) app get any error, close and reopen it. But when main (wpf) application is closing, winforms app have to be closed - in this case do not reopen winforms app. – user1617141 May 22 '14 at 14:55
  • 1
    add a background process that checks if the tray app is running every few seconds. Set the tray app so it terminates on error. Then in the WPF app, if it notices the tray app is not running, then simply restart it. – Michael Kniffen May 22 '14 at 15:32
  • Well, I have background process to set current time, so I can check there is process is still alive, and if not, restart. Simply and good imho reply. – user1617141 May 23 '14 at 08:21
0

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.