Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to know when Windows is "settled" after startup?

I'm writing an application that will have an option to run on Windows Startup.

I can't stand when applications bog my PC down before it has really settled, and this is a non-critical application. I'd like my application to politely wait for all other startup items to finish and settle so that the user's PC becomes responsive before it starts doing any work.

How would I go about detecting this condition? I suppose I could traverse the registry and look for all startup processes to be running, or use a longish timer. I'm just hoping there is another less hackish trick I might use.

EDIT: The application has a UI and cannot run as a service. It does have a tray mode. It does some image rendering.

like image 707
bopapa_1979 Avatar asked May 08 '12 15:05

bopapa_1979


4 Answers

It seems this is the type of thing best left in the hands of the user, and Windows already offers a good option to control this, at least for Windows Services.

If this is a Windows Service, the service can be configured to start Automatic (Delayed Start)

Perhaps that is the best option?

If you don't want to go that route, or if your program is not a Windows Service, detecting when Windows is "settled" is quite subjective and error prone. You could monitor Disk IO as a somewhat reliable indicator. Disk IO is typically the bottleneck during startup (especially for systems without SSD OS drives). You might get a reasonable approximation of "settledness" waiting for the Disk IO (as indicated by performance counters) to drop below a certain threshold. You would want to wait no longer than a configurable amount of time and then start anyhow, because some systems may boot and start performing unrelated, long-running tasks that tax the IO subsystem.

like image 45
Eric J. Avatar answered Oct 23 '22 13:10

Eric J.


A 'longish' timer is the best route. I wouldn't touch the registry with a bargepole.

One thing you will want to consider though is what happens if a user wants to launch your program themselves? Obviously you don't want to be causing the delay then.

I assume your application has a UI? If not, you could consider a Service with its start type set to "Automatic (Delayed)" on Vista/7

Please also see this related question: How to create delay startup application in c#?

A final point, if your program is non-critical, and you are worried it might slow things down, consider lowering the priority of the application.

like image 191
KingCronus Avatar answered Oct 23 '22 13:10

KingCronus


Two suggestions:

  1. Use a low-priority thread to do your work. After all, it's a low priority, right? This alone has a good chance of successfully minimizing the start-up impact.
  2. If you really want to wait for things to settle - just check the processor & memory utilization rather than doing all that registry reading or waiting for an arbitrary amount of time. Instructions on that are here: How to get the CPU Usage in C#?

Good luck!

like image 1
James Avatar answered Oct 23 '22 15:10

James


Rather late (by four years) to the party here but in case anyone else stumbles on this Q&A like I did ... I asked a similar question but my focus was on knowing when things settle down before the user logs-in. I understand the OP's app is run from the desktop and has UI elements, etc. But if you check that thread you'll see that there Is a way to tell if Windows has finished loading services. So a service can run at startup, clear a "done" flag, wait for services to start, then set that flag. Now when the user logs-in, if the system is still on its way up, the app will know and not rush to initialize the UI. If the "done" flag is set, the UI can "feel comfortable" about starting in the knowledge that most or all of the other apps have done their thing. It's possible that the user will start yet another app after logging in. Thus, the done flag will be set but things could still be going on. This is the place where other suggested checks like CPU usage would be of value.

like image 1
TonyG Avatar answered Oct 23 '22 13:10

TonyG