คำตอบ events on application shutdown accidently

ตอบทั้งหมด

  • 17 สิงหาคม 2555 11:25
     
      มีโค้ด

    Hi,

    could you please explain what this means? You need an event if the application shuts down? What does "accidently" mean? An exception is raised?

    Write your own Sub Main. After showing and closing the main form, you can execute finalization code.

    Public Class Main
    
       Shared Sub Main()
    
          Application.EnableVisualStyles()
          Application.SetCompatibleTextRenderingDefault(False)
          Application.Run(New Form1)
          MsgBox("this is the end")
    
       End Sub
    
    End Class
    

    You can also handle exceptions in sub main.


    Armin

  • 21 สิงหาคม 2555 3:59
     
     

    What does "accidentally" mean?

    power cable unplugged, windows OS system error, out of memory ......

    this case may no event be fired when accidentally occurred

  • 21 สิงหาคม 2555 13:01
     
     
    Oh, ok, I didn't know.

    Armin

  • 21 สิงหาคม 2555 13:57
     
     
    Thanks for your try to help . this case may no solution.... ;)

    fsze88

  • 22 สิงหาคม 2555 8:07
    ผู้ดูแล
     
     คำตอบ มีโค้ด

    Here, try this little snippet, parse event viewer with linq.

    Option Strict on
    Imports System.Text
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim EventViewer As New System.Diagnostics.EventLog("System")
                Dim EVLType As Type = GetType(EventLogEntry)
                Dim EVENTID As Long = 41
                Dim Query As IEnumerable(Of Object) = From ev In EventViewer.Entries Where CType(ev, EventLogEntry).InstanceId = EVENTID Select ev
                Dim Results As New List(Of EventLogEntry)
                For Each Obj As Object In Query.ToList
                    Results.Add(DirectCast(Obj, EventLogEntry))
                Next
                'Msg all of the power failure entries(or do something else hint hint)
                For Each EventLogEntry As EventLogEntry In Results
                    MsgBox(EventLogEntry.TimeGenerated.ToLongDateString & vbCrLf & vbCrLf & EventLogEntry.Message)
                Next
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    End Class
    


    If you want something you've never had, you need to do something you've never done.

  • 22 สิงหาคม 2555 8:21
    ผู้ดูแล
     
     คำตอบ มีโค้ด

    Hi,

    I want to know an event fired on application shutdown accidentally

    or to just make a method of checking if the app was properly closed do something simple maybe like the example below, or make a registry entry etc... Do something outside of ram on the form load event, and on the form close event, destroy what you did. and if the app was not closed correctly, it will still exist next time you run the app. And you will know...

    Option Strict on
    Imports System.Text
    Public Class Form1
        Dim TempFile As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\appopen.dat"
        Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            My.Computer.FileSystem.DeleteFile(TempFile)
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If My.Computer.FileSystem.FileExists(TempFile) Then MsgBox("The application closed unexpectedly last time it was ran!")
            My.Computer.FileSystem.WriteAllText(TempFile, "AppOpened", False)
        End Sub
    End Class


    If you want something you've never had, you need to do something you've never done.