C# - How to know when Windows is “settled” after s

2019-04-20 19:25发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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!



回答4:

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.