.net framework - C# windows 7 run exe before computer shutdown

07
2014-07
  • user1743843

    I want to run an executable that saves an XML file on a local drive before the computer (running on Windows 7) gets shut down.

    I know it is possible to run scripts before shutdown. This can be configured in Local Group Policy Editor under Computer Configuration --> Windows Settings --> Scripts(Startup/Shutdown).

    I have tried to put my executable in the shutdown property, but it does not work. How can I run an executable before the computer gets shut down? How can I hook up the shutdown event?

  • Answers
  • Remus Rusanu

    Launching a process when the shutdown was initiated is a bad idea. At this stage the most likely outcome is to get back the error 1115 ERROR_SHUTDOWN_IN_PROGRESS.

    A much better way is to install a service and register the service to receive shutdown notifications via RegisterServiceCtrlHandlerEx, see shutting down:

    Service applications receive shutdown notifications in their handler routines. To register a service control handler, use the RegisterServiceCtrlHandlerEx function.

    A C# service would do this by setting CanShutdown to true and then handling the OnShutdown event. It is important to be service, not an application, as shutdown can occur when no session is logged on and/or multiple sessions could be running when the shutdown occurs.


  • Related Question

    performance - Warm-up .NET application
  • boj

    We have speed problems in corporate enviroment.

    Most of the notebooks are strongly loaded: the start-up time of Excel is 3-5 minutes, Outlook 10-15 minutes etc.

    Our application has the same start-up time, instead of the normal 10-20 seconds. If once it started, after a program restart it works fine.

    How is it possible to warm-up our application?

    My first tought was to write a small script and run it right before our application startup:

    • connect to the local SQL Server and get some data
    • preload assemblies
    • ???

    But I think it's not enough - or totally a mistake.

    Edit:

    They have IBM Thinkpad X40 notebooks with 1GB RAM. Specialized Windows XP installed (I think this + background installers are the root of all our problems).


  • Related Answers
  • gridzbi

    If the machines are this grossly under-powered then I'd make the argument to your superiors that the problems lie in the companies IT provision to staff. I'd say any development time spent on this would be ill-spent.

  • sventevit

    Maybe a bit naive, but still... format & reinstal Windows :-)

  • Rick

    What do you mean strongly loaded?

    From your symptoms the problem might be that they do not have enough RAM. In which case when you start your applications they will have to swap previously running applications into virtual memory.

    When you close that application and restart it straight away, you will have free RAM so it will restart quickly.

    But it will do no good if you do something else in the meantime, i.e. open Outlook, close Outlook, open Excel - do some work, then if you open Outlook again it will not open quickly, i.e. your original suggestion to try and automatically open and close your application (at bootup?) won't help.

  • David Basarab

    Here is a nice article about speeding up an application start time.

    Improving Application Startup Time

    Over the few past months, the CLR performance team met with several customers to investigate performance issues in some of their applications. One recurring problem was client application startup time. So in this column, I'll present lessons we learned analyzing these applications.

    Planning for Performance Your success in reaching your performance goals depends on the process you will be using. A good process can help you achieve the level of performance you need. These four simple rules will help:

  • Florian Doyon

    Perhaps this is a roaming profile issue? I don't know about your program, but Excel and Outlook make significant use of the User folder, which is probably stored somewhere on your corporate network for each Active Directory account. My guess is that either the network or the storage system is too slow for your 300+ users.

  • Klay

    You or someone with some influence should be able to make a solid case to management for lost productivity because of the exorbitant startup times. It should be clear that in a very short time the cost of upgrading RAM would pay for itself in increased productivity. Heck, if you have to wait 15 minutes four times a day (a conservative estimate, I'm sure), you lose 12.5% of your workday EVERY DAY. Multiply this by 300 laptops, and...

    Oh--you must work for a federal agency!

  • Psycogeek

    What are the metrics you see via Task Manager ?

    Which Process is consuming the most CPU ?

    Which Processes are consuming RAM ?

    Is there an anti virus running in the background doing a full scan every time a computer starts ?

    how many Page Faults by which processes ?

    Whats the Configuration of Computers and which Softwares ?

  • Seasoned Advice (cooking)

    As Simon hinted at above, this shouldn't be the type of issue you try to work around using a warmup script. I'd suggest serious hardware and software upgrades. I mean 15 minutes to start outlook?