tray icon doesnt appear when app is run from process.start - c#

I have a C#/winforms application that runs minimized to the system tray. If I double click the EXE for this app it runs as it is supposed to, I see the process in task manager and the icon appears in the system tray. I also have a windows service which acts as a watchdog for the other app. If the winforms app gets closed the service restarts is using process.start
If the app is started from the service using process.start, firing off the same EXE file the process runs but the tray icon doesnt appear.
Just to be clear the winforms app puts the tray icon in place not the windows service.
Any idea why the app would react differently to a process.start than to a double click?
Edit: I may have partially answered my own question. The service is running as local system. Not sure some run as local system would be able to add an icon to my users system tray in the same manner a double click would. Does this sound like I am on the right track?

Yes, you're on the right track. Services run in a different session (session 0). If they open a window (which is highly discouraged), they are now called "interactive services". In that case, Windows pops up a dialog (see MSDN blog for a screenshot).
Similar things probably happen to the tray icon. Since you don't have a window, you don't get the popup dialog for interactive services, but the tray icon still exists in session 0, so you cannot see it.
If you're on Windows 8, interactive services have been disabled (MSDN) completely.

Related

How to launch in system tray icons a MVC app

I'm developing a MVC application using .NET 6.0. I am publishing the app with the following configuration:
The app, so far, is launched by double-clicking the .exe, it shows the classic "cmd-style" window.
Now the requirement is to start the portable app minimized into a system tray notification area in Windows (if this is not possible, I was looking for a method to deploy the app as a service which runs in background).
How can I achieve this? Thank you.
To obtain access to the system tray, you need a message pump and a target window. In other words, a regular command-line executable doesn't cut it. Basically you need to have an executable that creates a window (can be an invisible one) and then the main thread must pump Windows messages.
I'll say this much for now becuase the provided information is insufficient and explaining all possible scenarios would be too long a response.

Winforms tray application and WM_CLOSE

How to write a WinForms application that would not have any visible element except from a tray icon and at the same time allow the application to be closed by Windows Installer if the file is in use? As I understand it the Windows Installer sends a WM_CLOSE message to application's main window. I assume that the "main window" is indicated by MainForm property of ApplicationContext. Also it seems to me that for this mechanism to work the form must have been shown at least once.
I found some workarounds like creating an empty form and setting its opacity to 0 (to minimize flicker) then showing and hiding, but describing this as a nasty hack would heavily underestimate its ugliness.
Windows Installer does not send any WM_CLOSE messages. This might help:
http://haishibai.blogspot.com/2010/02/complete-tutorial-patch-restart-your.html
It's a slightly different scenario but might get you started if you want Windows Installer to tell your app when it should close down.
On recent operating system versions with Restart Manager a tray app will be detected. Back on Windows XP it wasn't detected.
In the WiX world there is a CloseApplication utility custom action that will send WM_CLOSE to the app window.

WPF Application not opening when clicking on the Desktop Icon

I am developing a Wpf application. On installing my application, shortcut icons are created in the desktop and the program menu.
When i click the desktop icon for the time, my application opens. Now i minimize my application in the tray/task bar. On clicking my desktop icon, my application is not maximized. It still minimized in the tray/task bar. For ex, you can take a look at Skype, adobe reader etc..
Requirement - where ever the application icon is present in the PC such as desktop or program menu or etc., my application should be maximized when the user clicks the shortcut icon.
A desktop link will open a new application every time you click on it. If you want your application to open only once and change position/size of the original one once you start a new one, you will have to do this yourself:
Once you application starts, check if another version is already running.
Send your running application a message.
End itself.
If there was no other application started, run normally and liste for the message.
On arrival of the message, check if minized and if so, maximize (or change size/position to whatever you need).
Try to implement it and if you have a very specific problem with any of the steps, come to SO and ask a very specific question about it :)

How do I hide the Windows system tray in C#?

I have a working Windows app that appears as an icon in the Windows system tray. When I click on my icon in the system tray it opens my application window, but the expanded system tray doesn't disappear. How do I tell the expanded system tray to hide itself once I've launched my application?
You don't. The notification area (often called "system tray") manages that stuff by itself, and applications shouldn't mess with it (that's why there are no APIs to affect substantially its behavior); read e.g. here.

How to make an application automatically start at Windows start?

I am new to .NET, I have made a small app. I want that this app should start when we start Windows, also how to make this app be present like a system tray icon if we minimize it. The app is a Windows form application.
As far as having it start automatically you can make it a windows service or you can simply place it in the windows startup folder and it will automatically launch it. For the System tray you should look into the NotifyIcon control.
For starting: couple of options..
1: Use scheduled tasks - accessed through control panel / adminstrative tasks
2: Make a shortcut to your exe in the startup folder in your start menu
For system tray: refer to this question: What's the proper way to minimize to tray a C# WinForms app?
in addition to what #Jesus said
you can also add a registry entry for start up. Windows service wouldn't do if your app is requiring the tray icon.
Best bet IMO is to make an installer project and configure the installer to have it start with windows when it's installed. Let the installer figure it out.

Categories