Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

How to monitor a Process in windows 2003 and Terminate IT.

All,

I'm looking to write something in VB.Net (2005) that will monitor a process on a windows 2003 server and if it runs for more than X amount of time, terminate it.

In a nutshell, the server has a program that is launched from a remote PC to run a report and occasionally locks up. Since the user doesn't have access to the server, they keep trying to launch the report again and again but finally give up. I know from past experience I can terminate the process using pskill or taskill, I'm just looking for a way to automate it. Basically, if the "x process" runs for more than a minute, kill it...

Thanks in advance,

Mike
0
MiketheD
Asked:
MiketheD
  • 2
1 Solution
 
MatthiasVanceCommented:
Will the application run on the server itself or from a remote location?

Kind regards,

Matthias Vance
0
 
MiketheDAuthor Commented:
Matthias,

Thanks for your reply... On the server...
0
 
MatthiasVanceCommented:
Here's some example code. You just need to adjust it a bit, add a timer and a few minor changes.

Kind regards,

Matthias Vance
Public Class frmMain
 
    Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal handle As IntPtr, ByVal ms As Int32) As UInt32
 
    Private Sub CheckProcess(ByVal processName As String, ByVal allowedTime As Integer)
        Dim pl() As Process = Process.GetProcessesByName(processName)
        If (pl.Length > 0) Then
            Dim p As Process = pl(0)
            If WaitForSingleObject(p.Handle, 5000) > 0 Then
                p.Kill()
            End If
        End If
    End Sub
 
    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CheckProcess("calc", 5000)
    End Sub
End Class

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now