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

4

11

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.

Ramiform answered 8/5, 2012 at 15:38 Comment(7)
This article might be of some interest to youTavy
There's no specific signal for this. I suppose you could sample the performance counters for cpu load and disk transfers.Fluorescence
OK, I had to add the following comment: I would NEVER create an application that defaults to run on startup. You have to set that explicitly. If you do that in your application, and there isn't some business person saying they'll fire you if you don't, you're a bad person!Ramiform
@Tavy - Thanks. I fully understand that the startup option is one of convenience and does not improve startup performance. Thank you for the excellent link, though.Ramiform
Is this a service, or by "startup" do you really mean "logon"? If the latter, see Raymond Chen's answer to #10362483Kenley
@vlad the link to the article is brokenCorot
@thepirat000: ah, msdn blogs have broken all the links again :-( It's actually blogs.msdn.microsoft.com/oldnewthing/20050311-00/?p=36223 nowTavy
Y
5

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.

Yasmineyasu answered 8/5, 2012 at 15:41 Comment(1)
Accepted answer for thoroughness. I think I'll wind up waiting for CPU usage to settle and then using a thread with the lowest possible priority and allowing that priority to be configurable. I can also appreciate your remark about the registry. That would have been a last resort :<)Ramiform
U
3

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.

Uprear answered 8/5, 2012 at 15:42 Comment(1)
This is also an excellent approach. I acutally wound up using a combination of Disk IO polling, CPU usage polling, and a max time to wait.Ramiform
C
2

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!

Carpenter answered 8/5, 2012 at 16:7 Comment(0)
A
1

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.

Astrict answered 18/1, 2016 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.