Link to home
Start Free TrialLog in
Avatar of kevandju
kevandjuFlag for United States of America

asked on

How to check if cscript.exe process is running...

I would like to create a VB .NET app that checks every hour to see if cscript.exe is running and if it is not then run c:\scripts\test.vbs.  I know I need to use a timer and I have it set for 60 minutes, I just don't know what the commands are for when the timer elapses.
SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of amyhxu
amyhxu

Alex, that sure is a brilliant idea!

kevandju, in case you are still wondering what event to use when using a timer:

you can start the timer by calling: timer1.Enabled = True ; and stop it by: timer1.Enabled = False

Private Sub timer1_Tick(sender As Object, e As System.EventArgs) Handles timer1.Tick

    Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("cscript.exe")
    If processes.Length = 0 Then
          System.Diagnostics.Process.Start("c:\scripts\test.vbs")
    ElseIf processes.Length > 1 Then
          'If you only want one instance of cscript.exe running, do something
    End If

End Sub
Avatar of kevandju

ASKER

Okay that definitely works, but I'm not sure what to do if the process is already running.  How do I tell the process just to end and do nothing?

For the timer issue I believe you must do a satelite application, that will be allways active and triggers itself every hour and check that.

If you need that to run even when no user is logged to the computer, then you must create a Windows Service.
If you want this to happen only when a user is logged, then you must start the "satelite" application on the Startup or on the registry.

When you've decided what you need, just put amyhxu's code inside mine, and make it run...

--- // ---

As your question/doubt is based only on how to stop the current process and leave the old one, I assume you got everything figured out except that.
But that's the easyes part! :)
Just do nothing!

If you start your application on the Sub Main, check for the existance of the process and if false launch the new process, is false do nothing.
Ended that, if you have no form to hold the current thread loop, the application will end.
If you have a form, just close it, the application will end.

Alex :p
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is what I have, I have the timer elapsing every hour.  I sped it up to 30 seconds for testing and what I get is my script launching repeatedly over and over every 20 seconds no matter how many instances of cscript.exe are already running.  Am I doing something wrong?

Private Sub Timer3_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer3.Elapsed

        Dim NoPrevInstance As Boolean
        Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
        If Not NoPrevInstance Then
            Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("cscript.exe")
            If processes.Length = 0 Then
                System.Diagnostics.Process.Start("c:\wgen\wxftp.vbs")
            ElseIf processes.Length > 1 Then 'Only leave one instance of the process running
                For i As Integer = 0 To processes.Length - 2
                    If Not processes(i).HasExited Then
                        processes(i).Kill()
                    End If
                    processes(i).Close()
                Next
            End If
        End If

    End Sub

Where do you have that code?
What control holds the timer control?

Alex :p
This is my whole VB app.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
       
        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Timer1 As System.Timers.Timer
    Friend WithEvents Timer2 As System.Timers.Timer
    Friend WithEvents Timer3 As System.Timers.Timer
    Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
    Friend WithEvents AxWebBrowser2 As AxSHDocVw.AxWebBrowser
    Friend WithEvents AxWebBrowser3 As AxSHDocVw.AxWebBrowser
    Friend WithEvents AxWebBrowser4 As AxSHDocVw.AxWebBrowser

   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Timer1 = New System.Timers.Timer
        Me.Timer2 = New System.Timers.Timer
        Me.Timer3 = New System.Timers.Timer
        Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser
        Me.AxWebBrowser2 = New AxSHDocVw.AxWebBrowser
        Me.AxWebBrowser3 = New AxSHDocVw.AxWebBrowser
        Me.AxWebBrowser4 = New AxSHDocVw.AxWebBrowser
        CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.Timer2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.Timer3, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxWebBrowser2, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxWebBrowser3, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.AxWebBrowser4, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 30000
        Me.Timer1.SynchronizingObject = Me
        '
        'Timer2
        '
        Me.Timer2.Enabled = True
        Me.Timer2.Interval = 10800000
        Me.Timer2.SynchronizingObject = Me
        '
        'Timer3
        '
        Me.Timer3.Enabled = True
        Me.Timer3.Interval = 20000
        Me.Timer3.SynchronizingObject = Me
        '
        'AxWebBrowser1
        '
        Me.AxWebBrowser1.Enabled = True
        Me.AxWebBrowser1.Location = New System.Drawing.Point(-140, -280)
        Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser1.Size = New System.Drawing.Size(1035, 768)
        Me.AxWebBrowser1.TabIndex = 0
        '
        'AxWebBrowser2
        '
        Me.AxWebBrowser2.Enabled = True
        Me.AxWebBrowser2.Location = New System.Drawing.Point(0, 0)
        Me.AxWebBrowser2.OcxState = CType(resources.GetObject("AxWebBrowser2.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser2.Size = New System.Drawing.Size(1035, 768)
        Me.AxWebBrowser2.TabIndex = 1
        '
        'AxWebBrowser3
        '
        Me.AxWebBrowser3.Enabled = True
        Me.AxWebBrowser3.Location = New System.Drawing.Point(0, 0)
        Me.AxWebBrowser3.OcxState = CType(resources.GetObject("AxWebBrowser3.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser3.Size = New System.Drawing.Size(1035, 768)
        Me.AxWebBrowser3.TabIndex = 2
        '
        'AxWebBrowser4
        '
        Me.AxWebBrowser4.Enabled = True
        Me.AxWebBrowser4.Location = New System.Drawing.Point(0, 0)
        Me.AxWebBrowser4.OcxState = CType(resources.GetObject("AxWebBrowser4.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxWebBrowser4.Size = New System.Drawing.Size(1035, 768)
        Me.AxWebBrowser4.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScale = False
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(638, 478)
        Me.ControlBox = False
        Me.Controls.Add(Me.AxWebBrowser1)
        Me.Controls.Add(Me.AxWebBrowser2)
        Me.Controls.Add(Me.AxWebBrowser3)
        Me.Controls.Add(Me.AxWebBrowser4)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.Timer2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.Timer3, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxWebBrowser2, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxWebBrowser3, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.AxWebBrowser4, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Cursor.Hide()
        AxWebBrowser1.Silent = True
        AxWebBrowser2.Silent = True
        AxWebBrowser3.Silent = True
        AxWebBrowser4.Silent = True
        AxWebBrowser1.Navigate("http://www.msn.com")
        AxWebBrowser2.Navigate("file:///C:/wgen/html/1-radar.html")
        AxWebBrowser3.Navigate("file:///C:/wgen/html/2-forecasts.html")
        AxWebBrowser4.Navigate("file:///C:/wgen/html/3-current.html")
    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
        On Error Resume Next

        Static intBrowserID As Integer = 0

        Select Case intBrowserID
            Case 0
                AxWebBrowser1.BringToFront()
                AxWebBrowser4.Refresh2()
            Case 1
                'AxWebBrowser1.Refresh2() Refresh with Timer2 every 3 hours
                AxWebBrowser2.BringToFront()
            Case 2
                AxWebBrowser2.Refresh2()
                AxWebBrowser3.BringToFront()
            Case 3
                AxWebBrowser3.Refresh2()
                AxWebBrowser4.BringToFront()
        End Select
        intBrowserID += 1
        If intBrowserID > 3 Then
            intBrowserID = 0
        End If

    End Sub

    Private Sub Timer2_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer2.Elapsed
        On Error Resume Next

        AxWebBrowser1.Refresh2()

    End Sub

    Private Sub Timer3_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer3.Elapsed

        Dim NoPrevInstance As Boolean
        Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
        If Not NoPrevInstance Then
            Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("cscript.exe")
            If processes.Length = 0 Then
                System.Diagnostics.Process.Start("c:\wgen\wxftp.bat")
            ElseIf processes.Length > 1 Then 'Only leave one instance of the process running
                For i As Integer = 0 To processes.Length - 2
                    If Not processes(i).HasExited Then
                        processes(i).Kill()
                    End If
                    processes(i).Close()
                Next
            End If
        End If

    End Sub
End Class





If you are using timer, my code itself will work for you (use timer.Tick instead of timer.Elapsed). Alex and I kind of provided two different ways of checking instances of a process.
yeah...
You are using System.Timers.Timer, change it for the System.Windows.Forms.Timer.
The problems may come from there.

Alex :p
For your reference (two ways of checking instances of a process, not for solving the problem you are having):
http://www.freevbcode.com/ShowCode.Asp?ID=5333
http://www.freevbcode.com/ShowCode.asp?ID=5845
Alex is right, you should use System.Windows.Forms.Timer.
Here is my new code and I still have the same problem

Friend WithEvents Timer3 As System.Windows.Forms.Timer
Me.Timer3 = New System.Windows.Forms.Timer(Me.components)
Me.Timer3.Enabled = True
Me.Timer3.Interval = 20000

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer3.Tick

        Dim NoPrevInstance As Boolean
        Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
        If Not NoPrevInstance Then
            Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("cscript.exe")
            If processes.Length = 0 Then
                System.Diagnostics.Process.Start("c:\wgen\wxftp.bat")
            ElseIf processes.Length > 1 Then 'Only leave one instance of the process running
                For i As Integer = 0 To processes.Length - 2
                    If Not processes(i).HasExited Then
                        processes(i).Kill()
                    End If
                    processes(i).Close()
                Next
            End If
        End If

    End Sub
My code alone should work (don't mix Alex's and mine). This doesn't mean that Alex's code wasn't right. Just that my code already does the whole thing.
Okay I found the problem, the process name had to be cscript and not cscript.exe.  This is working perfectly now!!

    Private Sub Timer3_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer3.Tick

        Dim NoPrevInstance As Boolean
        Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
        If Not NoPrevInstance Then
            Dim processes() As Diagnostics.Process = Diagnostics.Process.GetProcessesByName("cscript")
            If processes.Length = 0 Then
                System.Diagnostics.Process.Start("c:\wgen\wxftp.bat")
            ElseIf processes.Length > 1 Then 'Only leave one instance of the process running
                For i As Integer = 0 To processes.Length - 2
                    If Not processes(i).HasExited Then
                        processes(i).Kill()
                    End If
                    processes(i).Close()
                Next
            End If
        End If

    End Sub