kevandju
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.ElapsedEvent Args) 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.GetPro cessesByNa me("cscrip t.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
Private Sub Timer3_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEvent
Dim NoPrevInstance As Boolean
Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
If Not NoPrevInstance Then
Dim processes() As Diagnostics.Process = Diagnostics.Process.GetPro
If processes.Length = 0 Then
System.Diagnostics.Process
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
ASKER
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.ICon tainer
'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.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Cont ainer
Dim resources As System.Resources.ResourceM anager = New System.Resources.ResourceM anager(Get Type(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.ISup portInitia lize).Begi nInit()
CType(Me.Timer2, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.Timer3, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.AxWebBrowser1, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.AxWebBrowser2, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.AxWebBrowser3, System.ComponentModel.ISup portInitia lize).Begi nInit()
CType(Me.AxWebBrowser4, System.ComponentModel.ISup portInitia lize).Begi nInit()
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 30000
Me.Timer1.SynchronizingObj ect = Me
'
'Timer2
'
Me.Timer2.Enabled = True
Me.Timer2.Interval = 10800000
Me.Timer2.SynchronizingObj ect = Me
'
'Timer3
'
Me.Timer3.Enabled = True
Me.Timer3.Interval = 20000
Me.Timer3.SynchronizingObj ect = Me
'
'AxWebBrowser1
'
Me.AxWebBrowser1.Enabled = True
Me.AxWebBrowser1.Location = New System.Drawing.Point(-140, -280)
Me.AxWebBrowser1.OcxState = CType(resources.GetObject( "AxWebBrow ser1.OcxSt ate"), System.Windows.Forms.AxHos t.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( "AxWebBrow ser2.OcxSt ate"), System.Windows.Forms.AxHos t.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( "AxWebBrow ser3.OcxSt ate"), System.Windows.Forms.AxHos t.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( "AxWebBrow ser4.OcxSt ate"), System.Windows.Forms.AxHos t.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.AxWebBr owser1)
Me.Controls.Add(Me.AxWebBr owser2)
Me.Controls.Add(Me.AxWebBr owser3)
Me.Controls.Add(Me.AxWebBr owser4)
Me.FormBorderStyle = System.Windows.Forms.FormB orderStyle .FixedSing le
Me.Icon = CType(resources.GetObject( "$this.Ico n"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormS tartPositi on.CenterS creen
CType(Me.Timer1, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.Timer2, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.Timer3, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.AxWebBrowser1, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.AxWebBrowser2, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.AxWebBrowser3, System.ComponentModel.ISup portInitia lize).EndI nit()
CType(Me.AxWebBrowser4, System.ComponentModel.ISup portInitia lize).EndI nit()
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("fi le:///C:/w gen/html/1 -radar.htm l")
AxWebBrowser3.Navigate("fi le:///C:/w gen/html/2 -forecasts .html")
AxWebBrowser4.Navigate("fi le:///C:/w gen/html/3 -current.h tml")
End Sub
Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEvent Args) 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.ElapsedEvent Args) Handles Timer2.Elapsed
On Error Resume Next
AxWebBrowser1.Refresh2()
End Sub
Private Sub Timer3_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEvent Args) 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.GetPro cessesByNa me("cscrip t.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
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.ICon
'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.Debugg
Me.components = New System.ComponentModel.Cont
Dim resources As System.Resources.ResourceM
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.ISup
CType(Me.Timer2, System.ComponentModel.ISup
CType(Me.Timer3, System.ComponentModel.ISup
CType(Me.AxWebBrowser1, System.ComponentModel.ISup
CType(Me.AxWebBrowser2, System.ComponentModel.ISup
CType(Me.AxWebBrowser3, System.ComponentModel.ISup
CType(Me.AxWebBrowser4, System.ComponentModel.ISup
Me.SuspendLayout()
'
'Timer1
'
Me.Timer1.Enabled = True
Me.Timer1.Interval = 30000
Me.Timer1.SynchronizingObj
'
'Timer2
'
Me.Timer2.Enabled = True
Me.Timer2.Interval = 10800000
Me.Timer2.SynchronizingObj
'
'Timer3
'
Me.Timer3.Enabled = True
Me.Timer3.Interval = 20000
Me.Timer3.SynchronizingObj
'
'AxWebBrowser1
'
Me.AxWebBrowser1.Enabled = True
Me.AxWebBrowser1.Location = New System.Drawing.Point(-140,
Me.AxWebBrowser1.OcxState = CType(resources.GetObject(
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(
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(
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(
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.AxWebBr
Me.Controls.Add(Me.AxWebBr
Me.Controls.Add(Me.AxWebBr
Me.Controls.Add(Me.AxWebBr
Me.FormBorderStyle = System.Windows.Forms.FormB
Me.Icon = CType(resources.GetObject(
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormS
CType(Me.Timer1, System.ComponentModel.ISup
CType(Me.Timer2, System.ComponentModel.ISup
CType(Me.Timer3, System.ComponentModel.ISup
CType(Me.AxWebBrowser1, System.ComponentModel.ISup
CType(Me.AxWebBrowser2, System.ComponentModel.ISup
CType(Me.AxWebBrowser3, System.ComponentModel.ISup
CType(Me.AxWebBrowser4, System.ComponentModel.ISup
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("fi
AxWebBrowser3.Navigate("fi
AxWebBrowser4.Navigate("fi
End Sub
Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEvent
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.ElapsedEvent
On Error Resume Next
AxWebBrowser1.Refresh2()
End Sub
Private Sub Timer3_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEvent
Dim NoPrevInstance As Boolean
Dim MyMutex = New Threading.Mutex(True, "BIMutex", NoPrevInstance)
If Not NoPrevInstance Then
Dim processes() As Diagnostics.Process = Diagnostics.Process.GetPro
If processes.Length = 0 Then
System.Diagnostics.Process
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
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
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 .
ASKER
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.compon ents)
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.GetPro cessesByNa me("cscrip t.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
Friend WithEvents Timer3 As System.Windows.Forms.Timer
Me.Timer3 = New System.Windows.Forms.Timer
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.GetPro
If processes.Length = 0 Then
System.Diagnostics.Process
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.
ASKER
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.GetPro cessesByNa me("cscrip t")
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
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.GetPro
If processes.Length = 0 Then
System.Diagnostics.Process
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
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.GetPro
If processes.Length = 0 Then
System.Diagnostics.Process
ElseIf processes.Length > 1 Then
'If you only want one instance of cscript.exe running, do something
End If
End Sub