none
Shut down the program RRS feed

  • Question

  • I want a function when the power is off the computer. And restart the device after an opening.
    Clarification and the time and duration at which the program was closed

    Provided that it is not linked to user shutdown
    Tuesday, October 31, 2017 6:30 PM

Answers

  •  Well,  i am still not sure if i really understand but,  maybe this is similar to what you want to do.  You can try it in a new form project with 1 Timer and 1 Label added to the form.

     You will also need to add 2 application settings as shown here...

     

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            If Not My.Settings.UserClosed Then 'check if the user closed the program last
                If My.Settings.WasRunningAt = Date.MinValue Then 'if the date is the minimum date value, it has never been set. this indicates first time app has ever been run.
                    Label1.Text = "First time being run..."
                Else
                    Dim ts As TimeSpan = Now - My.Settings.WasRunningAt
                    If ts.TotalHours < 1.0 Then
                        Label1.Text = "Has been " & ts.TotalMinutes.ToString & " minutes since power failure."
                    Else
                        Label1.Text = "Has been " & ts.TotalHours.ToString & " hours since power failure."
                    End If
                End If
            Else
                Label1.Text = "User closed the app last time."
            End If
    
            My.Settings.UserClosed = False 'reset to indicate user did not closed the app
    
            Timer1.Interval = 1000
            Timer1.Start()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Timer1.Stop()
            My.Settings.UserClosed = True 'set true to indicate the user closed the app
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.WasRunningAt = Now 'keep this updated to current date and time
            My.Settings.Save() 'save the settings just in case power failure
        End Sub
    End Class
    
     

     Try it and see if it is even close to what you want to do.  If it is,  i'm sure it needs a few fixes for different scenarios.  I am getting out of here for tonight though.  8)


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Thursday, November 2, 2017 2:20 AM
    Wednesday, November 1, 2017 2:19 AM

All replies

  •  Your question is not clear.  Do you want this information about the Computer,  or a specific program,  or maybe some device you have hooked to the computer?

    If you say it can`t be done then i`ll try it

    Tuesday, October 31, 2017 6:39 PM
  • I want information about my program by showing the time since the shutdown of the computer as a whole to run the program
    Tuesday, October 31, 2017 11:42 PM
  • I want information about my program by showing the time since the shutdown of the computer as a whole to run the program

    Ok... let me see if i understand you right.  You want your program to know how long it has been since the computer was shut down last?

    If you say it can`t be done then i`ll try it

    Wednesday, November 1, 2017 12:34 AM
  • What I do not know is that my software is running and suddenly I shut down my computer due to power failure

    When the machine is turned on and the program is running

      The time is calculated since the shutdown time is run
    Wednesday, November 1, 2017 12:50 AM
  •  Well,  i am still not sure if i really understand but,  maybe this is similar to what you want to do.  You can try it in a new form project with 1 Timer and 1 Label added to the form.

     You will also need to add 2 application settings as shown here...

     

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            If Not My.Settings.UserClosed Then 'check if the user closed the program last
                If My.Settings.WasRunningAt = Date.MinValue Then 'if the date is the minimum date value, it has never been set. this indicates first time app has ever been run.
                    Label1.Text = "First time being run..."
                Else
                    Dim ts As TimeSpan = Now - My.Settings.WasRunningAt
                    If ts.TotalHours < 1.0 Then
                        Label1.Text = "Has been " & ts.TotalMinutes.ToString & " minutes since power failure."
                    Else
                        Label1.Text = "Has been " & ts.TotalHours.ToString & " hours since power failure."
                    End If
                End If
            Else
                Label1.Text = "User closed the app last time."
            End If
    
            My.Settings.UserClosed = False 'reset to indicate user did not closed the app
    
            Timer1.Interval = 1000
            Timer1.Start()
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Timer1.Stop()
            My.Settings.UserClosed = True 'set true to indicate the user closed the app
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.WasRunningAt = Now 'keep this updated to current date and time
            My.Settings.Save() 'save the settings just in case power failure
        End Sub
    End Class
    
     

     Try it and see if it is even close to what you want to do.  If it is,  i'm sure it needs a few fixes for different scenarios.  I am getting out of here for tonight though.  8)


    If you say it can`t be done then i`ll try it

    • Marked as answer by ahmeddc Thursday, November 2, 2017 2:20 AM
    Wednesday, November 1, 2017 2:19 AM
  • What I do not know is that my software is running and suddenly I shut down my computer due to power failure

    When the machine is turned on and the program is running

      The time is calculated since the shutdown time is run

    Well if you power down your computer without shutting down the program first then IMO the program will not know to save the time. However if you use a timer in a program you can then set it to store time every second within a user setting (user scoped variable) of the program (if that last time in the user setting is 1 second or so less than the next time checked) , save the user setting each time then save it if it isn't auto saved which I don't remember if that occurs when a user setting is written to, and then when the app runs again write code on launch to get the user settings variable of the last time and if that is say like 2 minutes different than the PC's current time it can save that in some other user setting (possibly a user scoped variable of a Generic TypeOf String I believe) or something. User settings are per user and are stored in an XML file for each user using an app.

    I don't know if a user scoped variable for Time exists or not or if a TypeOf Time variable is also available.


    La vida loca

    Wednesday, November 1, 2017 5:15 PM