user1617141
user1617141

Reputation: 115

Right way to run, kill and keep process running

In my application, which using another application (run in tray) to print receipts I need to do those three things:

First point is quiet easy, I just simply

Process.Start("_ReceiptPrinter.exe");

And process working ;)

But now, the two other issues:

Unfortunately, I can still see icon in tray, and process is still running.

Upvotes: 2

Views: 1393

Answers (2)

user4472163
user4472163

Reputation:

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.

Upvotes: 0

Michael Kniffen
Michael Kniffen

Reputation: 346

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

proc.Kill();

Upvotes: 3

Related Questions