Detect whether a Windows reboot was due to Windows updates - c#

A few applications on my PC have been doing a detection wether a restart was done by Windows Update or not. This is observable due to them restarting after the automatic Windows Update reboot.
This is very helpful since those applications reload changes, even unsaved changes or restore tabs (in case of a browser). Examples of applications that do this:
Google Chrome
Microsoft Visual Studio
Microsoft Paint
In all cases the applications save the state they were in before the reboot.
(My PC actually woke up from a standby state automatically, and rebooted itself while instaling updates. Too bad it didnt go back to standby after doing this.)
My question is: How do I programmatically detect this kind of reboot? It seems to be reliably detectable before the reboot.
The question How can I get the Windows last reboot reason might seem like a duplicate, but no answer said anything about updates.
I code in C#, so an answer in C# would be helpful, though I can read C and C++ too, for example.

See Application Restart:
An application can use Application Recovery and Restart (ARR) to save data and state information before the application exits due to an unhandled exception or when the application stops responding
and:
or if the computer needs to restart as the result of an update.
The applications aren't detecting the restart reason after the fact - they've been architected to use this API. There isn't (so far as I'm aware) a managed API for this.
A couple of clicks away is Saving data and application state when application is being closed due to a software update. Again, no managed API.

Normally when Windows Reboots, it performs a restart by using shutdown.exe and the /g flag which saves the state of all registered running programs before restart. What you want to do is to Register your application for restart. There is a good sample with documentation for this by MSDN. It is available in C# and C++.

Related

C# WinForms application runs slow from mapped network drive over VPN

We have a Winforms application that is located on a server.
In other words, the people start the application by a shortcut that points to the executable on the intranet. There where never any performance issues with this approach up till now.
The problem is that over a VPN the application loads extreme slow.
It takes 1 minute and 40 seconds just to show the main form.
All code in the main form in the Load and Show events are disabled to test the performance, and it still takes this long time just to load the form.
Also the mapped directory is set a trusted by windows.
The question is what could cause this slow loading over VPN ?
It seems to me that just loading the main form without any code is already slow.
The application uses DevExpress so there are lots off DLL's to load. Can that be the cause of this problem ?
I know this is not a good question for SO but I need some ideas in which direction to look for this problem.
I found this question here but the problem there was due to a bug in their code so it does not helps me
Deploy the application as ClickOnce. When the application is first run it will be slow as the application will download, then install in the Users[user]\AppData folder. From then on as long as the application hasn't been redeployed it'll run from the local copy after checking with the network deployed version.
You can choose to have the application install locally so it has a start menu entry and can be removed with add/remove programs or you can have it only available online which means there's no start menu entry and it should be started from the setup.exe (I would assume this is the option you would want to go for so your users still start it from the VPN network location.

How best to diagnose an issue when debugging C# application results in VisualStudio stalling

I am working in VisualStudio 2010 SP1 on an application server system written in C#. (Yes this is a legacy application.) The system is a large multi-threaded server application that provides a large SOAP API along with newer REST APIs for other portions of the system.
For a long time I have noticed that occasionally when starting the system in debug mode both the server and VisualStudio would stall. Neither UI would respond and calls to the server APIs would block. This situation could be recovered from only by halting the server using Task Manager, after which VisualStudio would complain with an error.
Recently this problem became persistent, that is it happens every time. The lock up occurs at various times, that is I have not been able to identify a specific time when the system locks up, it may (often) happen as soon as the server is started, other times it may take a minute or so, but always shortly after startup, both VisualStudio and the server lock up.
If I try to do something in the VisualStudio UI, e.g. pressing the Pause button, I will get the "Microsoft Visual Studio Delay Notification" icon in the notifications section of the Windows task bar.
I have done some investigation on the web and have tried the following unsuccessfully:
Clean rebuild
Removing all build products from the directory structure
Doing a clean checkout in a new directory structure
resetiis
Removing the .suo files
Resetting the VisualStudio settings
I have also tried creating both mini & full dumps of both the application and devenv, but I have not been able to understand them well enough to find any issue.
What would you do to try to identify the issue?
The solution to my issue turned out to be working on the program using VS2015. This is not a wonderful solution since we still build in VS2010, but it at least allowed me to proceed. We are not sure exactly what caused the issue.

Performance issue for WPF application when is running without right clicking and choosing "Run as administrator"

I have a WPF application which will be used on tablets with Windows 10.
In my development environment, the application runs very fast. However, if I run the application on tablet without admin rights, the application responds very slow : all animations are slow, the scrolling is not smooth, and touch problems are visible (cannot move the caret). If the application has admin rights all things are working fine.
I tried to disable the logs, I tried to decrease the FPS, nothing seems to help.
My application is connected to a sqlite database, but even without accessing the database is very slow. There are no other connections that may require admin rights.
What could be the problem?
EDIT: Also in my dev environment the app is running slower without admin rights. I was running the VS2015 with admin rights and for that reason the application worked fine.
WPF has settings under HKEY_CURRENT_USER that can disable hardware acceleration:
https://msdn.microsoft.com/en-us/library/aa970912(v=vs.110).aspx
If the Disable Hardware Acceleration Option or the Use Reference Rasterizer Option are set, a WPF application will run slowly for that user.
Try to trace your app with Process Monitor. Run 2 traces for app running with and without admin rights. Filter your app events only via Tools->Process Tree... -> right click on your process -> Add to Include filter and compare these trace logs (or just post them here). Pay attention on gaps between registry key readings or network access.
That literally could be anything.
Mark Russinovich make a presentation almost every year about finding some interesting cases. Here's one of them.
P.S. I'm sorry to post that as an answer, but I don't have enough points to add comments right now.
To offer an answer for people that might face the same issue, now, after I've searched a lot for something helpful, I can say that this problem it is mainly a problem related to rights.
If you encounter this issue, try to run Visual Studio without admin rights and try to isolate the region where the application begins to act very slow (or the place were it crashes) . In my case, wasn't the database, or the log files, but a file in AppData which was used by an external assembly. I've seen that, every time that dll was called, the application was freezing and using dotTrace, I've managed to find which file it is used. After setting the rights (r/w) on that file, everything was back to normal.

Debugging non-responding application in VS

I'm now working on some pretty big application with really nasty code. The problem is that from time to time the whole GUI of the app is freezing and stops responding (and after some time Windows wants to take the app down). I have a really big problem to even look where to debug the app, mostly because when this thing happens and I pause it in the VS debugger, it highlight the line:
CardWindow.ShowDialog();
It's not helpfull at all. Also when this thing happens it tends to grow the app size in the memory, ocasionally throwing OutOfMemoryException.
I also checked whether it might be some kind of an infinite loop in one of created threads, but VS shows me that only the main thread is active, and the rest are either ended or not running at all (the thread implememtation is also really poorly written).
The application uses remote objects within local networks, and some threads are used to transfer photos and other data from clients to server, but I don't know how to check if this is what it's causing it. But when the server app hangs, the clients are still fully capable to connect with it.
Short info of how it's made: the app uses Firebird engine to store its data. The DB is on the server machine, and the clients are connected via local LAN to it using standard Firebird C# library, and the app instances communicates with each other using remote object (the server is also a client).
It's the second week of debugging and I'm getting pretty desperate as I'm getting out of ideas of even how to check what's wrong. The fact that the bug appears randomly also doesn't help.
Can anyone give me some ideas of how to find a trace of this bug?
Using C# .NET 4.5 and WPF with Visual Studio Ultimate 2013 and Firebird 2.5
I'd suggest using a profiling tool to assist in finding the problem
What Are Some Good .NET Profilers?
In addition to #DerekTomes. The System.Windows.ShowDialog method, Described in: Window.ShowDialog Method, all other windows are disabled and return only when the window is closed. Check to make sure the window is eventually closed(this.Close() in the CardWindow.xaml.cs or whatever .cs file) or use CardWindow.Show() instead of CardWindow.ShowDialog() so other windows remain running while the CardWindow is running.
I finally figured it out.
When the function that usually hanged the app after 30th run launched, I could see that some of it ran (the window's title changed as it was supposed to). I've found this function and after every single line put:
this.Title = "1";
SomeFunction();
this.Title = "2";
AnotherFunction();
this.Title = "3";
// and so on
I localized what was causing the problem; there was a function that changed the GUI significantly (lots of foo.Visibility and foo.Opacity) and ran in some weird loop. It was fine for the first 20 times to run it, but after 30th it slowed down the app, and after 40th - caused crash.
It now runs only once when it needs to and the problem does not appear. I suppose it caused GUI memory overflow of some kind.
Anyway, thank you for your answers - I ran memory profiling, but it slowed my app down to that point it was impossible to use it.

How to debug a hanging WPF application?

I have a WPF application that is hanging on one user's machine. Other users aren't affected. The app hangs both running natively out of the development environment and whilst running under debug. The problem can be reproduced reliably - it simply hangs whatever the user attempts to do. Running under debug offers no clues, no exception is thrown, the app simply stops responding.
What options do I have for debugging this? Are there any external tools - things like sysinternals suite for example - that can help? Are there any Visual Studio debugger tricks or tips that might provide a bit more info?
Yes, you can use mdbg.exe to attach to the hanging process (a PID) and when you are there press w to see the stack trace.
btw. the hanging could possibly be caused by the corrupted font cache so before you try the mdbg try to find the instruction on how to clear the WPF's font cache.
Since this is a rather general description only general pointers:
hit "pause" in the debugger and see where it hangs
patch the system + drivers to the latest "stable level"
check any 3rd-party assemblies you are using
perhaps there is some quirk regarding version and/or configuration and/or dependency
use sysinternal processmon to check what the app does (which files are accessed etc.)
check network connectivity and configuration (proxy, DNS, firewall etc.) if your app uses anything (like a network share or DB or internet access etc.)
check RAM (perhaps the machine is paging like crazy...)
if your app is multi-threaded there can be exceptions "lurking" => try adding global handlers to get a clue (see here)
Font problems can be another issue to account for... see here
check for antivirus etc. => these can sometimes create strange behaviour
anything of the above could give you a clue what's going on.
Check your hardware acceleration for the machine. Remember WPF uses direct hardware access for rendering when available. You can try reducing your acceleration, updating your video drivers, etc.
What operating system is this running under? If Windows 7 - is Aero enabled?

Categories

Resources