Tray NotifyIcon missing if application starts with system - c#

I have a C# (VS 2012 Express) targeting .NET framework 2.0 which uses the NotifyIcon component to create a tray icon.
There is a shortcut to the application in the users Start Menu\Startup folder which results in the icon appearing in the tray when windows starts, most of time.
In some situations however, the executable launches on system startup but the icon is missing from the tray. I don't a have a set of reliable steps to reproduce this issue however.
From reading this article, it looks like I should be listening to the shell started event and re-drawing my tray icon, however I'm unsure how to go about doing this, as .NET is managing the low level calls for me:
http://msdn.microsoft.com/en-us/library/windows/desktop/cc144179(v=vs.85).aspx
Any pointers would be greatly appreciated. Thanks!
Update: This problem only appears to happen when I have setup Windows to auto-logon to the default user account. If I manually logoff and then logon again my icon appears correctly.
Update: It appears that if I manually close and restart Explorer.exe, the icon is correctly redrawn.

I solved this problem be toggling the visibility of the NotifyIcon component (from false to true) at the end of my form constructor (after calls to InitializeComponents).

Related

Tray Application that runs for a long time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Writing a Windows system tray application with .NET
I want to develop a tray application. What this should do is it must keep looking for recently modified files.. once a file is get caught it a copy of that file to the web server. Therefore, user will be able to login to the website and later on user will be able to download those backing up files. what i want to know is how do i develop the tray application? Do i have to write a windows service for that? If so, i need a tray application to control the service (tray application must have sfunctionalities to start and stop service [Backup service], An option to choose the file extensions)
I'm using c#.
Thanks a lot in advance.
Sounds pretty straightforward.
Create a basic WinForms app. The app's main window should start "minimized" and should not show in the taskbar. But, it should have a "NotifyIcon" component, which allows the app to show in the system tray. Double-clicking the NotifyIcon would restore the window, show the window in the taskbar and hide the NotifyIcon until the window is minimized or closed again.
This application can do the file watching on its own, or it can edit the configuration used by a Windows service; either way would work. Either way, you need to set up a FileSystemWatcher (or several of them) that monitors files in the directories the user wants you to check. When a FileSystemWatcher sees a file change, it'll fire an event that you can handle by performing the upload to the web server.
Just as an aside to the whole design; if you're implementing an online backup system (which it appears that you are), I would make sure that the user has access to several past iterations of the file, in case they want to restore a previous version. You should also have virus-checking; the last thing a user would need is for a virus to wreck their files AND for your backup system to have backed up the wrecked files.
I personally wouldn't make it like a Windows Service, but would choose a simple application that "lives" on behind of system. It's easier to maintain and update, uninstall from the client (on his request). In short, for something simple I wouldn't go for Windows Service.
I would add a sys-tray component, with some menus.
You can have a look at WPF NotifyIcon from Philipp Sumi.
One of menu items can have an item that registers executable to run on startup of the OS.
You can achieve that with kind of code:
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Assembly curAssembly = Assembly.GetExecutingAssembly();
key.SetValue(curAssembly.GetName().Name, curAssembly.Location);
If for some reason, cause it's not very clear from your post, your application, indeed, goes to manage some serious amount of data on considerable production environment, go for WindowsService and manage all related to it stuff.
Hope this helps.

C# how to run applications on the Desktop?

Is it possible to run applications on the Windows Desktop? I mean... that it can only be seen in the system tray, and it should be able to run alongside the desktop.
I have no idea how to code it, please help me. I'm kind of new to these things, I am supposed to create something like a "Stardock Fence".
I have seen some examples, but they seem buggy, any strong alternative I could use?
Process.Start() can be used to start a windows application/console app from another win application. There are parameters that allow you to optionally hide the UI as well.
You want your application's windows to be always-on-bottom. In other words, your UI will always appear to be beneath any other open window and just above the Desktop's icons.
To accomplish that, see these related questions.
Once you have a window always on the bottom of the z-order, you'll probably want to remove the non-client window chrome (titlebar/min/max/close buttons) so that your UI can look like a more integrated part of the Desktop. There's plenty of examples around; Googling is left as an exercise for the reader.
Not entirely sure what you mean by background. I expect you mean a Windows Service which is a project type in visual studio, or you might (less likely) mean running a background thread.
Actually, if you want it in the system-tray, you don't want it entirely in the background.
If it was to be fully "in the background", then your best bet is to have it as a service.
System tray icons need a window, but you can just make it non-visible and non-taskbar and that's fine.
A common combo is a service that does the actual heavy-lifting, and a hidden-window application with a systray icon that reports on the service's status (possibly making that same window visible when further interaction is needed).
You want to run your application in the background? Is it on a windows machine? If so then you want to look into running your application as a windows service. Here's an msdn link:
Introduction to Windows Services
There's examples in the article I think - if not it's a good starting point. You can configure services to start automatically on startup of the machine etc. Your application will then run in the background.
Basically you craete your application as normal and then host it in a windows service rather than say a console app or a winforms app.

Notify icon not always appearing in the system tray after a setup, but still functional

First of all, when I start the application normally (double-clicking on the exe), it works perfectly: the notify icon is always appearing in the system tray. It also works well when the application is launched at the end of an msi Setup (Run exe after msi installation?).
However, when the application is launched from an msi running in quiet mode, my notify icon isn’t always appearing, but the application is functional: I can access the contextual menu with a keyboard shortcut. I tested on three computers running under Windows XP and the success rate is around 50%. I also tested on Windows7: it works perfectly.
I know that there are some issues with the notify icons during the startup in Windows XP, but I don’t know if it is related (http://www.google.ca/search?hl=fr&q=notify+icon+not+appearing+Windows+XP&aq=f&aqi=&aql=&oq=&gs_rfai=
So, I was wondering if any of you guys ever experienced this problem. Do you think it’s a Windows XP bug? Or is it related to who is launching the application (msiexec vs .exe)? I don’t think it could be an error in my code, since it’s working well when I start it directly.
You must realize that when you run silent ( /quiet /qn UILevel=3 ) that the InstallUISequence doesn't run, only the InstallExecuteSequence does. Therefore you need to schedule your custom action to run at the end of the InstallExecuteSequence when running silently so that your C# program will run and place itself in the tray.
For those that question this requirement, it's fairly normal to do this. As an SMS Admin I would push out packages silently that would shut down a tray app, uninstall the old version, install the new version and put the try app back. All this without the agent barely notice it was ever missing.

How do I keep .NET applications pinned to the Task Bar during AutoUpdate?

When an application deployed by ClickOnce AutoUpdate is automatically updated on Windows 7, the application becomes unpinnned from the taskbar. Is there a way to stop it from becoming unpinned?
When the ClickOnce application is not installed it most likely impossible to achieve this.
When it's installed; i'm not sure.
A ClickOnce application is downloaded to the users' temporary directory. When the application downloads the latest version, this version is stored in a new sub directory and not overwritten as which is the case with 'normal' application updates.
I don't know about keeping it from being unpinned but there is a way using a vbs script to pin an exe which isn't supposed to be doable by code:
Call AddToTaskbar("C:\temp\", "MyExe.exe")
Function AddToTaskbar (Path, File)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(Path)
Set objFolderItem = objFolder.ParseName(File)
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then
'WScript.Echo objVerb
objVerb.DoIt
End If
Next
End Function
This essentially relies on the right click menu of an exe having the "Pin to Taskbar" entry. It unfortunately makes it english specific unless someone has a list of all translations.
I'm not seeing this problem at all. I'm assuming you manually pinned the application to the task bar the first time you ran it.
Are you targeting .NET 3.5 (SP-1)? Are you having ClickOnce create the desktop shortcut for you or are you doing it programmatically? Does the desktop shortcut disappear?
Does it disappear from the taskbar every time an update is released, or just every so often?
RobinDotNet
I'm not sure how Windows 7 handles it, but I use code similar to this to copy the appref-ms to the startup folder on the start menu. I think my code (on my work machine and I'm off for the weekend) also has a check for if this is a new install so it does not change the shortcut if it is an update.
In Visual Studio 2010 on the publish tab. I clicked options, Selected Manifests and clicked Create desktop shortcut. This preserved by start menu shortcuts after an update was published and users installed the update. Previously the start menu shortcut would disappear and have to be re-pinned.

How to display NotifyIcon and SSDP Service running during AutoLogon

I've got an application (that is targetting .Net Framework 2.0) that is running on startup of the System, and I'm trying to get a NotifyIcon to display.
When my program starts up when a user either Runs it normally or is started as a child process after the system has already logged on everything is fine.
If my application starts up as the system is performing an AutoLogon using POSReady2009 (basically XP with Single User set). Then the NotifyIcon never becomes active.
If you subsequently check (in a timer) the .Visible of the Icon at any point later it always reports that it is visible = true.
If you disable the SSDPSrv and restart the Computer, the Icon displays correctly.
I have a sneaking suspicion this is related to .Net 3.5sp1 installed over the top of a .Net 2 system.
Is there some process that I should be following to ensure that my NotifyIcon is always available to the user.
I have setup RegisterWindowMessage("TaskbarCreated") but I don't get this message called, except when you forcilbly Kill Explorer.exe and restart it. Even so, a NotifyIcon interally registers for these notifications anyway, so it shouldn't be required.
I'm happy to stall the startup of my program, but once the program starts up, I expect that the icon shows correctly.
If there is a KB article that I cannot find detailing this I'd be happy with that too.
Hmm... that's odd.
This may not work, and it's possibly not the best way of doing it - but first of all try putting NotifyIcon1.Visible = True in the Load event.
If that isn't working why not try adding this into a timer...
NotifyIcon1.Visible = False
NotifyIcon1.Visible = True
This should then hide and show the icon everytime the timer ticks, at least then you can see if it's working. Maybe only run the timer 10 times and then it ends, that way the script should hide and show the icon 10 times by which time the system should be ready.
Let me know if this works - if not I'll have a scratchy beard moment and have another think!
Well it's kinda dumb but have endedup needing to modify the process of startup to deal with this issue.
Made the program it's own shortcut that get's placed into the Startup folder by our installer.
The program that triggers this no longer starts it automatically. Instead we inform the other program once we have started up (Dropping a trigger file).
The other program then monitors if the user closes us OR we just crash and automatically re-opens the program. [There is a proper process to follow if you wish to fully close down the system]
Inside the startup of the program we check to see if the SSDP service is available and Not set to Disabled.. if so, wait until this service has started. We then check that SQLServer is running.
We then prepare the NotifyIcon and set it's .visible= true and all is good.
Still have kept the RegisterWindowMessage in the event the user somehow kills windows explorer.

Categories

Resources