Results 1 to 17 of 17

Thread: Detect if program is not responding!

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Detect if program is not responding!

    I am hosting a little application called "cserver.exe" but sometimes it crashes for no reazon. sometimes it happens at night and it starts to be a problem.

    is there any way of detecting if cserver.exe stopped responding? and if so, kill it and re-open it. thanks.

    ps. it can be a timer that checks the application status every 3 minutes.

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    You might find something useful here.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Re: Detect if program is not responding!

    it didn't help since the other guy couldn't't figure it aswell

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    How about this?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Re: Detect if program is not responding!

    i don't really understand that one. where do i place cserver.exe so it can trace it? close it and re-open it if it's not responding

  6. #6
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    I guess it would be something like:

    vb Code:
    1. Option Explicit
    2.  
    3. 'API Constants
    4. Const SMTO_BLOCK = &H1
    5. Const SMTO_ABORTIFHUNG = &H2
    6. Const WM_NULL = &H0
    7. Const WM_CLOSE = &H10
    8. Const PROCESS_ALL_ACCESS = &H1F0FFF
    9. 'API functions
    10. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
    11. lpdwProcessId As Long) As Long
    12.  
    13. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
    14. ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    15.  
    16. Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _
    17. (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As _
    18. Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    19.  
    20. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    21. ByVal uExitCode As Long) As Long
    22.  
    23. Private modObjIE As Object
    24. Private modlngWndIE As Long
    25.  
    26. Private Sub cmdCheck_Click()
    27.     Dim lngResult As Long
    28.     Dim lngReturnValue As Long
    29.   lngReturnValue = SendMessageTimeout(modlngWndIE, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)
    30.     If lngReturnValue Then
    31.         MsgBox "Responding"
    32.     Else
    33.         MsgBox "Not Responding","Block tester"
    34.     End If
    35. End Sub
    36.  
    37. Private Sub cmdLaunch_Click()
    38.     Set modObjIE = Nothing
    39.     Set modObjIE = CreateObject("cserver.exe")
    40.      'cserver activities code
    41.       modObjIE.Visible = True
    42.  
    43.     modlngWndIE = modObjIE.hwnd
    44. End Sub
    45.  
    46. Private Sub cmdKill_Click()
    47.     Dim lngProcessID As Long
    48.     Dim lngReturnValue As Long
    49.     Dim lngProcess As Long
    50.     lngReturnValue = GetWindowThreadProcessId(modlngWndIE, lngProcessID)
    51.     lngProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lngProcessID)
    52.     lngReturnValue = TerminateProcess(lngProcess, 0&)
    53. End Sub

    That is after you add a reference to "cserver.exe" in to the project.
    Last edited by Nightwalker83; Apr 9th, 2011 at 09:21 PM. Reason: Fixed spelling!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Re: Detect if program is not responding!

    i can't figure it out from there, sorry for bugging you so much, but here is a change of plan. i just want this:

    i have a button and a textbox

    if i press the button it will put in the textbox 1 or 0
    1 if cserver.exe is running
    0 if cserver.exe is not responding

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    Quote Originally Posted by Jhivago View Post
    i have a button and a textbox

    if i press the button it will put in the textbox 1 or 0
    1 if cserver.exe is running
    0 if cserver.exe is not responding
    From what I can tell this would still require the code I posted above except instead displaying a message box saying whether or not the program was responding you would display the particular value in a text box.

    I have an idea try the code I posted above using the instructions from the page I linked to.

    # Start a new Standard EXE project in Visual Basic.
    # Form1 is created by default. Place three command buttons on the form and name them cmdCheck, cmdLaunch, and cmdKill. Change their Caption property to Check, Launch, and Kill, respectively.

    Add the code I posted above to the form.

    All I changed in the code I posted above was:

    vb Code:
    1. Set modObjIE = CreateObject("cserver.exe")

    from

    vb Code:
    1. Set modObjIE = CreateObject("InternetExplorer.Application")
    Last edited by Nightwalker83; Apr 9th, 2011 at 09:03 PM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Re: Detect if program is not responding!

    i get a syntax error, and this text is red:

    lngReturnValue = SendMessageTimeout(modlngWndIE, WM_NULL, 0&,
    0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)

    the one under Private Sub cmdCheck_Click()

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    I formatted the above code so that line of code would be:

    vb Code:
    1. lngReturnValue = SendMessageTimeout(modlngWndIE, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2010
    Posts
    15

    Re: Detect if program is not responding!

    i get a msgbox that tells me the program is not responding even if working.

  12. #12
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    Quote Originally Posted by Jhivago View Post
    i get a msgbox that tells me the program is not responding even if working.
    Did you add a reference to "cserver.exe" in the project?

    Edit:

    I created this and it seems to work.

    vb Code:
    1. Private Type STARTUPINFO
    2.       cb As Long
    3.       lpReserved As String
    4.       lpDesktop As String
    5.       lpTitle As String
    6.       dwX As Long
    7.       dwY As Long
    8.       dwXSize As Long
    9.       dwYSize As Long
    10.       dwXCountChars As Long
    11.       dwYCountChars As Long
    12.       dwFillAttribute As Long
    13.       dwFlags As Long
    14.       wShowWindow As Integer
    15.       cbReserved2 As Integer
    16.       lpReserved2 As Long
    17.       hStdInput As Long
    18.       hStdOutput As Long
    19.       hStdError As Long
    20.    End Type
    21.  
    22.    Private Type PROCESS_INFORMATION
    23.       hProcess As Long
    24.       hThread As Long
    25.       dwProcessId As Long
    26.       dwThreadID As Long
    27.    End Type
    28.  
    29.    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    30.       hHandle As Long, ByVal dwMilliseconds As Long) As Long
    31. 'API Constants
    32. Const SMTO_BLOCK = &H1
    33. Const SMTO_ABORTIFHUNG = &H2
    34. Const WM_NULL = &H0
    35. Const WM_CLOSE = &H10
    36. Const PROCESS_ALL_ACCESS = &H1F0FFF
    37. 'API functions
    38. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, _
    39. lpdwProcessId As Long) As Long
    40.  
    41. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
    42. ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    43.  
    44. Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" _
    45. (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As _
    46. Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    47.  
    48. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    49. ByVal uExitCode As Long) As Long
    50.  
    51.    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    52.       lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
    53.       lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    54.       ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    55.       ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
    56.       lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    57.       PROCESS_INFORMATION) As Long
    58.  
    59.    Private Declare Function CloseHandle Lib "kernel32" _
    60.       (ByVal hObject As Long) As Long
    61.  
    62.    Private Declare Function GetExitCodeProcess Lib "kernel32" _
    63.       (ByVal hProcess As Long, lpExitCode As Long) As Long
    64.  
    65.    Private Const NORMAL_PRIORITY_CLASS = &H20&
    66.    Private Const INFINITE = -1&
    67.       Dim proc As PROCESS_INFORMATION
    68.       Dim start As STARTUPINFO
    69.  
    70.  
    71.    Public Function ExecCmd(cmdline$)
    72.    
    73.       ' Initialize the STARTUPINFO structure:
    74.       start.cb = Len(start)
    75.  
    76.       ' Start the shelled application:
    77.       ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
    78.          NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
    79.  
    80.       ' Wait for the shelled application to finish:
    81.          ret& = WaitForSingleObject(proc.hProcess, INFINITE)
    82.          Call GetExitCodeProcess(proc.hProcess, ret&)
    83.          Call CloseHandle(proc.hThread)
    84.          Call CloseHandle(proc.hProcess)
    85.          ExecCmd = ret&
    86.    End Function
    87.  
    88.    Sub Form_Click()
    89.       Dim retval As Long
    90.         Dim lngResult As Long
    91.     Dim lngReturnValue As Long
    92.     'Replace the path and app.name.type with that of the application you want to use
    93.       retval = ExecCmd("C:\Users\User\Desktop\cserver.exe")
    94.         lngReturnValue = SendMessageTimeout(modlngWndIE, WM_NULL, 0&, 0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)
    95.     DoEvents
    96.     If lngReturnValue Then
    97.         MsgBox ("Responding")
    98.     Else
    99.     'Close the host and the client if the client does not respond
    100.         Call GetExitCodeProcess(proc.hProcess, ret&)
    101.         Call CloseHandle(proc.hThread)
    102.         Call CloseHandle(proc.hProcess)
    103.        Unload Me
    104.     End If
    105.    End Sub

    I created the above using a mixture of code from previous code above and the code here.
    Last edited by Nightwalker83; Apr 10th, 2011 at 12:46 AM. Reason: Adding more!
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Detect if program is not responding!

    Using NightWalker83's code in Post #12 as a basis, here's a (trimmed down) version that will start and then monitor the program every second (you can easily change the Timer interval)
    Code:
    Option Explicit
    '
    ' Assumes a Timer Control is drawn on the Form named Timer1
    ' On clicking on the Form, this Program will start C:\Project1.Exe 
    ' and monitor it until it terminates
    '
    Private Type STARTUPINFO
          cb As Long
          lpReserved As String
          lpDesktop As String
          lpTitle As String
          dwX As Long
          dwY As Long
          dwXSize As Long
          dwYSize As Long
          dwXCountChars As Long
          dwYCountChars As Long
          dwFillAttribute As Long
          dwFlags As Long
          wShowWindow As Integer
          cbReserved2 As Integer
          lpReserved2 As Long
          hStdInput As Long
          hStdOutput As Long
          hStdError As Long
    End Type
    
    Private Type PROCESS_INFORMATION
          hProcess As Long
          hThread As Long
          dwProcessId As Long
          dwThreadID As Long
    End Type
    
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
          hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
          lpApplicationName As String, ByVal lpCommandLine As String, ByVal _
          lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
          ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
          ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
          lpStartupInfo As STARTUPINFO, lpProcessInformation As _
          PROCESS_INFORMATION) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" _
          (ByVal hObject As Long) As Long
    
    
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private proc As PROCESS_INFORMATION
    Private start As STARTUPINFO
    
    
    Private Function ExecCmd(cmdline$)
    Dim ret As Long
    ' Initialize the STARTUPINFO structure:
    start.cb = Len(start)
    ' Start the application:
    ret = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
             NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)
    ExecCmd = ret
    End Function
    
    Sub Form_Click()
    Dim retval As Long
    Dim lngResult As Long
    Dim lngReturnValue As Long
    'Replace the path and name with that of the application you want to use
    retval = ExecCmd("C:\Project1.exe")
    Timer1.Enabled = True
    End Sub
    
    Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 1000
    End Sub
    
    Private Sub Timer1_Timer()
    Dim lngResult As Long
    Dim lngReturnValue As Long
    Dim ret As Long
    ret = WaitForSingleObject(proc.hProcess, 500)
    If ret = 0 Then
        MsgBox "Not Responding", , "Monitor"
        Call CloseHandle(proc.hThread)
        Call CloseHandle(proc.hProcess)
        Unload Me
    End If
    End Sub
    This is the program to be monitored: (C:\Project1.exe)
    Code:
    Option Explicit
    
    Private Sub Command_Click()
    Dim x As Long
    Do While True
        x = x + 1
    Loop
    End Sub
    When you click the button in the above it will run for a time and then fail with an Overflow Error. When you acknowledge the error, the monitor will detect the fact and issue a Message.

    Mind you, I think it would be better in the long run to determine why cserver.exe fails 'for no reason' - there's always a reason.

  14. #14
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Detect if program is not responding!

    My guess is that the program enters an infinite loop or it deadlocks somewhere, rather than just terminating all the time.

    The weird thing they seem to be stabbing around at above only works for an ActiveX EXE that exposes an hWnd property on its default class (if any). That's what IE does for its Application class.

    You could simply Shell() the program, grab the PID returned, lookup the process handle from that via an API call. Then open the process to monitor for completion as well as look up the hWnd based on the PID by enumerating all windows. That hWnd could be used to SendMessageTimeout the WM_NULL message.

    Without threading you can't do all of this perfectly since the SendMessageTimeout is a long-running sync call the way they're using it, but they might shorten the timeout and check for both things (process terminated and "window ping" working) based on a Timer tick.


    Better to just fix the program though.

  15. #15
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Detect if program is not responding!

    Quote Originally Posted by Doogle View Post
    When you click the button in the above it will run for a time and then fail with an Overflow Error. When you acknowledge the error, the monitor will detect the fact and issue a Message.
    I have tested this with a program I am currently creating although, it restarts the target program regardless of if it were closed correctly or as a result of a crash. Is there a way to detect whether it was a crash that closes the program or the user that closed the program?
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  16. #16
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,894

    Re: Detect if program is not responding!

    You could do something like this for crash checking:

    When the program you are monitoring starts up, set a registry value to 1. E.g. SaveSetting "MyApp", "Startup", "CrashCheck", 1
    In the Form_Unload event of the main form, delete that registry value. E.g. DeleteSettings "MyApp", "Startup", "CrashCheck"

    In your watchdog program, if the registry value is 1 (GetSetting("MyApp", "Startup", "CrashCheck", 0) and the program being monitored is not running, then you can assume that it crashed. Delete the registry value and restart the program.

  17. #17
    Lively Member Stupidiot's Avatar
    Join Date
    Apr 2011
    Location
    India
    Posts
    95

    Re: Detect if program is not responding!

    U can create a txt file to check this:
    Every 3 minutes - Timer-60000*3 - the main program “cserver.exe” will write the current time in a file:

    Code:
    WriteAlive()
    Open App.Path & "\Live.txt" For Output As #1
    Print #1, Time$
    Close #1
    Code:
    Private Sub Load () - main program cserver.exe load 
    If App.PrevInstance = True Then
    Unload Me
    Exit Sub
    End If
    Shell App.Path & "\Monitor.exe "
    End Sub
    Code:
    Private Sub Unload() - main program cserver.exe unload
    Open App.Path & "\Log.txt" For Output As #1
    Print #1, "User Closed" & Time$
    Close #1
    KillProcess "Monitor.exe" 'No need to Monitor
    End Sub
    Every 1 minutes - Timer-60000*1- the Monitor program “Monitor.exe” will check the same file and if necessary, kill & Reopen cserver.exe:

    Code:
    CheckAlive()
    Dim TextLine, X
    Open App.Path & "\Live.txt" For Input As #1
       Line Input #1, TextLine
    Close #1
    X = DateDiff("n", TextLine, Time$)
    If X > 3 Then
    KillProcess "cserver.exe" 'Kill
    Open App.Path & "\Log.txt" For Output As #1
    Print #1, "Stopped responding, Killed and re-opened" & Time$    
    Close #1
    Shell App.Path & "\cserver.exe" 'Re-Open
    End If
    Code:
    Private Sub Load () - Monitor program Monitor.exe load 
    If App.PrevInstance = True Then
    Unload Me
    Exit Sub
    End If
    End Sub
    Check this:
    http://www.vbforums.com/showthread.php?t=651961
    Last edited by Stupidiot; Jul 31st, 2011 at 04:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width