Starting apps at shutdown

I am writeing a simple app that displays a message telling the user to turn off the moniter etc before the user shuts down the system. Could someone exlpain how I can run the app at shutdown. The app also has to be iinstalled on a large network, is there anyway of installing it on all the machines at once.

Thanks



Answer this question

Starting apps at shutdown

  • GoingNCircles

    Hi Rob,

    Take a look at the ClickOnce delivery system. It's designed to enable applications to be installed from remote machines as and when they are updated.

    I am unsure as to what style of application you are creating, but if you can have a form loaded in it, a simple way to detect a system shutdown would be to handle the FormClosing event and check the value of the FormClosingEventArgs.ClosingReason property. One of the possible return values is WindowsShutdown. I have never used it myself so cannot contest to its accuracy/reliability/usage etc, but it might be a good starting point for you.

    If the above is not a suitable solution, please give a little more information on your application and I'll see if I can help some more.

    Richard


  • Mandragon

    Thanks,

    That seems to work. Another problem that's appeared is the machines that the app will be installed on is using an older version of the .Net framework and an error saying it can't find the framework. This is because it's looking for it in the folder "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727" which does not exist instead it should point to "C:\WINDOWS\Microsoft.NET\Framework\v framework version". Do you know of a way around this without installing the newer version of the framework.


  • DaPosh

    Hi Rob

    If your application has a dependency on version 2 of the framework then this must be installed on the machine your application runs on.

    Richard


  • MusicMan2006

    Here is a post of mine about running apps at shutdown. I haven't tried it out, but it might point you in the right direction:

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1132443&SiteID=1

    Joshua Morgan wrote:

    I believe you are asking how you can run an application when windows is shutting down

    If that is the case I think you can use the SystemEvents which is located in the Microsoft.Win32 Namespace. You can find more information about these events at http://msdn2.microsoft.com/en-us/library/z1bwe429.aspx

    You should learn about SessionEnded and SessionEnding.

    Try adding an event handler to a windows service that runs at startup. When you system start the service is ran and the event handler is registered. When the system is shut down the event will fire and run your application

    Imports Microsoft.Win32

    Public Class Service1

    Protected Overrides Sub OnStart(ByVal args() As String)

    AddHandler (SystemEvents.SessionEnding, AddressOf OnShuttingDown)

    End Sub

    Protected Overrides Sub OnStop()

    ' Add code here to perform any tear-down necessary to stop your service.

    End Sub

    Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)

    System.Diagnostics.Process.Start("c:\my.exe")

    End Sub

    End Class

    This code is not tested, but it will be a good starting place for you to experiment. Note that you can also cancel the system from shutting down in this event by making this modification:

    Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)

    e.Cancel = True

    System.Diagnostics.Process.Start("c:\my.exe")

    End Sub

    You could also cancel the shutdown initiated by the user, then run your application, then force a shutdown when you application has finished processing with something like this:

    Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)

    e.Cancel = True

    Dim myProcess As Process = System.Diagnostics.Process.Start("c:\my.exe")

    '

    ' Wait until it ends.

    '

    myProcess.WaitForExit()

    'Shut down now that my application if finished executing...

    System.Diagnostics.Process.Start("Shutdown", "/s")

    End Sub

    You will need to test this code out, but hope that helps put you in the right direction.



  • Starting apps at shutdown