How to display NotifyIcon and SSDP Service running during AutoLogon - c#

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.

Related

My Windows Store app is still running in debug mode after I close it

I'm writing my first Windows Store app (windows 8.1) and I notice that when I run it in debug mode, and I close the app (by clicking the x in the top right, or by dragging from the top of the screen to the bottom), it is still running in Visual Studio. My first question is, is this a problem? It seems like it's a problem.
I started from a template, I'm not doing anything with threads, and there is only one page (MainPage.xaml) at the moment. I have looked at questions which seem similar, in particular this one:
WPF App Doesn't Shut Down When Closing Main Window
but I am unable to get their suggestions to work.
When I add ShutdownMode="OnExplicitShutdown" to my app.xaml, I get these errors in my Error List:
The member "ShutdownMode" is not recognized or is not accessible.
The property 'ShutdownMode' was not found in type 'Application'.
Also I notice that there is no StartupUri specified, nor can I add one (same errors as above.)
The other suggestion was to override OnClosed in MainWindow.xaml.cs and close the application there. I have no MainWindow.xaml.cs; I have MainPage.xaml.cs instead, and it does not have an OnClosed.
The Application class is of type Windows.UI.Xaml.Application.
If I pause VS after closing the app, it takes me to this (generated) code:
#if !DISABLE_XAML_GENERATED_MAIN
public static class Program
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Windows.UI.Xaml.Build.Tasks"," 4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
static void Main(string[] args)
{
global::Windows.UI.Xaml.Application.Start((p) => new App()); //<==here
}
}
#endif
Can anyone tell me what's going on?
This is entirely normal, the debugger tells you what is really going on. All Modern UI apps work this way. Just check it out with Task Manager, Details tab. Observe how dismissing the window doesn't terminate the process, it just suspends it.
You don't just have a modern UI, you have modern operating system behavior as well. A user doesn't have enough information available to judge if terminating a process is actually useful. If the machine has plenty of resources then there isn't any point. Better to keep the process running so that when the user starts it again, it instantly wakes up. Which is nice, users like that.
Conversely, if the OS requires resources for another process and not enough are available than it will automatically terminate a process without the user's assistance. The life-cycle for Modern UI apps supports this. Nothing particularly revolutionary btw, mobile operating systems like Android do this as well. Also the way I use my desktop apps these days, I just leave them running. Until I run out of taskbar space, cleanup then. Annoying :)
Truly stopping the process is easy, just click the Red Button on the VS toolbar.

Process.Start("explorer.exe"); won't bring back taskbar

As of right now, I am working on a mock up OS via WinForms to use as a prop for movies.
Upon running the application, it kills explorer.exe so that you can't accidentally have the windows task bar show up during a shoot. The issue is, upon closing the mock OS I would like for explorer.exe to be started up again. However, Process.Start("explorer.exe"); brings up an explorer window, and does not re-instate the window taskbar.
I know for a fact, that task manager is more than capable of bringing back the window taskbar via typing "explorer.exe" under a new task, though I've had no luck finding command line arguments to pass to task manager.
Edit:
I'm running under Windows 7. As well, I'm going pretty in depth with this mock OS. I'm taking control of quite a few key presses that Windows uses. For this reason, I kill explorer.exe so that I can use key presses such as "Alt-Tab" and display a mock app switcher, etc. The app already runs in full screen, but it is still possible to have the underlying Windows GUI pop back up. I am essentially replacing explorer.exe with my own mock up explorer. Upon closing my custom explorer, I can't seem to get the regular Windows GUI to come back by launching explorer.exe via Process.Start();.
From here:
Try
Process.Start(Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));
It appears you must specify the full path to explorer to get the taskbar back.
From here:
Explorer must see some fulfilled conditions to launch as shell:
Explorer must not run (which includes Control Panel, for instance)
Explorer must see it is the actual shell - hence you need to replace that value before launching explorer.exe (could change it back a few seconds later)
Sometimes it seems (on newer Windows versions) it depends on the process that launches explorer.exe - if it is "known" to explorer.exe -- I don't have any more details for this part though (and you couldn't change it, unfortunately)

How to save data on Windows Store App Close?

i have the following problem. When the user closes the Windows store app, i want a text file with data to be saved.
What method should i write so that when closing the app a file gets saved ?
This article describes the application lifecycle of a Windows Store App.
If you look at the very first figure, you can see that there are only 3 events related to application lifecycle:
Activated - Raised when program first starts
Suspended - Raised when program is suspended (i.e. the user returns to the Start Screen or another app)
Resuming - Raised when the program is awakened from its suspended state.
The fourth transition - the one to the "Not Running" state - has no such notification event. The reason is: you don't really know when the app will fully close. Nor should you - Microsoft wants you to perform all of your state-saving logic in the transition from "Running" to "Suspended." In this way, they can free up resources when they deem necessary.
Even if the user forces the program to terminate (by right-clicking it and selecting "Close" from the task menu), you will enter the "Suspended" state for exactly 10 seconds before the program is terminated. So you can rest easy that your state-saving logic will always be executed.
you don't really know when the app will fully close. Nor should you...
I don't agree with this - Microsoft are copying this from Apple, and I don't know why, I never liked Apple's implementation either.
If your user makes changes to your App, then closes it using the keyboard or gesture, then restarts it say after 6 seconds, all changes are lost.
I don't see any way as a developer to get around this. Whoever decided that closed events (and exit buttons for that matter) are evil is an idiot.

Console application becomes ghost and does not show on task manager processes tab

My program uses MSSQL database and has been working fine. However, after making some changes including converting from VS2008 to VS2010, generating 32bit application. It starts having the following strange problem:
When I try to shutdown the console by clicking the system menu(X button), it does not shutdown, you can move the console around, you can also click on the left hand system menu such as properties, etc, but the program does not show up on the taskmanger's processes tab, therefore, there is no way to shut it down, but to restart box.
Also, while the program is in this ghost mode, I can actually start another instance. That means the ghost application already release most of the resources such as file handle, db etc.
The problem seems to occur after we have some issues with ms-sql server, such as time out exception. I simulate the situation by throwing an exception in the same db function call, but that does create the ghost situation. box is Windows Server Standard without Hyper-V, SP2, 32bit.
Can you reboot your computer after 'ghost application' appears?
If not, check this http://support.microsoft.com/kb/982551/en-us

Prevent Process 'A' from spawning Process 'B' which then shows up on top of what should be the "TopMost" Process 'C'

I have a windows form application which needs to be the TopMost. I've set my form to be the TopMost and my application works as I'd like it to except for in one case.
There is a 3rd party application (referred to as player.exe) that displays SWF movie files on a portion of the screen that popup on top of my application.
Using Process Monitor I determined that player.exe application calls
flash.exe <PositionX> <PositionY> <Width> <Height> <MovieFile>
in my case:
flash.exe 901 96 379 261 somemovie.swf
Since flash.exe is being spawned in a new process after my form has been set to the TopMost it is appearing on top of my application.
First thing I did was make my application minimize the player.exe main application window hoping that this would prevent the Flash from appearing also. But, unfortunately it doesn't... even with the window minimized whenever the flash movie starts it shows up at the pixel location (901,96). I then tried creating a timer to keep setting the form.TopMost property to true every 10ms. This sort of works but you still see a very quick blip of the swf file.
Is there some type of Windows API call which can be used to temporarily prevent player.exe from spawning child processes which are visible? I admit it sounds a little far fetched. But, curious if anyone else has had a similar problem.
Addendum:
This addendum is to provide a reply to some of the suggestions layed out in Mathew's post below.
For the emergency situation described in the comments, I would look at possible solutions along these lines:
1) How does the third party application normally get started and
stopped? Am I permitted to close it
the same way? If it is a service, the
Service Control Manager can stop it.
If it is a regular application,
sending an escape keystroke (with
SendInput() perhaps) or WM_CLOSE
message to its main window may work.
Easiest way to close the app is to CTRL-ALT-DEL, then kill process. -OR-
The proper way is to Hold ESC while clicking the left mouse button... then input your username and password, navigate some menu's to stop the player.
There is no PAUSE command... believe it or not.
I don't think using WM_CLOSE will help since minimizing the application doesn't. Would that kill the process also? If not, how do you reopen it.
2) If I can't close it nicely, am I permitted to kill it? If so,
TerminateProcess() should work.
I can't kill the process for two reasons. 1) Upon relaunch you need to supply username/password credentials... There may be a way to get around this since it doesn't prompt when the machine is rebooted but... 2) Whenever I kill the process in task manager it doesn't die gracefully and asks if you want to send an error report.
3) If I absolutely have to leave the other process running, I would try
to see if I can programmatically
invoke fast user switching to take me
to a different session (in which there
will be no competing topmost windows).
I don't know where in the API to start
with this one. (Peter Ruderman
suggests SwitchDesktop() for this
purpose in his answer.)
I got really excited by this idea... I found this article on CodeProject which provides a lot of the API Wrapper methods. I stopped implementing it because I think that in order for desktop's to work you must have explorer.exe running (which I do not).
EDIT2: On second thought... maybe explorer.exe isn't needed. I'll give it a try and report back.
Edit3: Was unable to get the code in that article working. Will have to put this on hold for a moment.
Answer Summary
As one might have expected, there is no simple answer to this problem. The best solution would be to problematically switch to a different desktop when you need to guarantee nothing will appear over it. I was unable to find a simple C# implementation of desktop switching that worked and I had a looming doubt that I would just be opening a whole new set of worms once it was implemented. Therefore, I decided not to implement the desktop switching. I did find a C++ Implementation that works well. Please post working C# virtual desktop implementations for others.
Setting the TopMost property (or adding the WS_EX_TOPMOST style to a window) does not make it unique in the system. Any number of topmost windows may be created by any number of applications; the only guarantee is that all topmost windows will be drawn 'above' all non-topmost windows. If there are two or more topmost windows, the Z-order still applies. From your description, I suspect that flash.exe is also creating a topmost window.
Aside from periodically forcing your window to the top of the Z-order, I think there is little you can do. Be warned, however, that this approach is dangerous: if two or more windows are simultaneously trying to force themselves to the top of the Z-order, the result will be a flickering mess that the user will likely have to use the task manager to escape.
I recommend that your program not attempt to meddle with other processes on the computer (unless that is its explicit purpose, e.g. a task manager clone). The computer belongs to the user, and he may not value your program more highly than all others.
Addendum:
For the emergency situation described in the comments, I would look at possible solutions along these lines:
How does the third party application normally get started and stopped? Am I permitted to close it the same way? If it is a service, the Service Control Manager can stop it. If it is a regular application, sending an escape keystroke (with SendInput() perhaps) or WM_CLOSE message to its main window may work.
If I can't close it nicely, am I permitted to kill it? If so, TerminateProcess() should work.
If I absolutely have to leave the other process running, I would try to see if I can programmatically invoke fast user switching to take me to a different session (in which there will be no competing topmost windows). I don't know where in the API to start with this one. (Peter Ruderman suggests SwitchDesktop() for this purpose in his answer.)
You can use the Process class to start flash.exe directly - and use an appropriate ProcessStartInfo settings to show the window in a hidden state - or with a WindowStyle of hidden or minimized.
You could also consider using the SetWindowsHookEx API to intercept the process start API calls, and when the process is flash.exe run some code to restore you window to top-most status.
Matthew's answer is excellent, but I suspect you may be asking the wrong question. Why does your application need to be topmost? If you're trying to create a kiosk or some such, then topmost is not the way to go.
Edit: After reading your response to Matthew's comment, I'd suggest creating a new desktop and switching to it before displaying your alert. (See CreateDesktop and SwitchDesktop in MSDN.)

Categories

Resources