By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,188 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,188 IT Pros & Developers. It's quick & easy.

Process question

P: n/a
I have a class which starts a process and redirects it's output and input.
My class have a method which starts the process and another which stops it.
How can I check if the process have been already started, so I don't try to
start it again, and how to check if the process is not running so i don't
try to stop again?

code:
Public Class ProcEx
Private p As Process
Public Sub StartProcess()
p = New Process
With p.StartInfo
.FileName = "cmd"
.Arguments = ""
.CreateNoWindow = True
.ErrorDialog = False
.RedirectStandardError = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = Application.StartupPath
End With
p.Start()
End Sub
Public Sub StopProcess()
Dim RetVal As String
If Not p.HasExited Then
p.Kill()
End If
p.Close()
'p.Dispose()
End Sub
End Class

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Nikolay,

Try Process.GetProcessesByName(). It will return to you a Process array
of all processes with the name you provide.

If the array is not empty, then you know the process has already been
started and you can take a reference to the old process rather than
create a new one.

---
Dim aProcesses() as Process
aProcesses = Process.GetProcessesByName("yourexecutable")

If aProcesses.Length > 0 Then
' process has been started before:
p = aProcesses(0)
Else
p = New Process
' initialize it here.
End If
----
Regards,
-Adam.

Nikolay Petrov wrote:
I have a class which starts a process and redirects it's output and input.
My class have a method which starts the process and another which stops it.
How can I check if the process have been already started, so I don't try to
start it again, and how to check if the process is not running so i don't
try to stop again?

code:
Public Class ProcEx
Private p As Process
Public Sub StartProcess()
p = New Process
With p.StartInfo
.FileName = "cmd"
.Arguments = ""
.CreateNoWindow = True
.ErrorDialog = False
.RedirectStandardError = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = Application.StartupPath
End With
p.Start()
End Sub
Public Sub StopProcess()
Dim RetVal As String
If Not p.HasExited Then
p.Kill()
End If
p.Close()
'p.Dispose()
End Sub
End Class


Nov 21 '05 #2

P: n/a
thanks
I have a question
I am using my app to run instance of cmd.exe
what if there is another cmd.exe started outside of my process?
"Adam Goossens" <ad**********@users.sourceforge.net> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi Nikolay,

Try Process.GetProcessesByName(). It will return to you a Process array of
all processes with the name you provide.

If the array is not empty, then you know the process has already been
started and you can take a reference to the old process rather than create
a new one.

---
Dim aProcesses() as Process
aProcesses = Process.GetProcessesByName("yourexecutable")

If aProcesses.Length > 0 Then
' process has been started before:
p = aProcesses(0)
Else
p = New Process
' initialize it here.
End If
----
Regards,
-Adam.

Nikolay Petrov wrote:
I have a class which starts a process and redirects it's output and
input.
My class have a method which starts the process and another which stops
it.
How can I check if the process have been already started, so I don't try
to start it again, and how to check if the process is not running so i
don't try to stop again?

code:
Public Class ProcEx
Private p As Process
Public Sub StartProcess()
p = New Process
With p.StartInfo
.FileName = "cmd"
.Arguments = ""
.CreateNoWindow = True
.ErrorDialog = False
.RedirectStandardError = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = Application.StartupPath
End With
p.Start()
End Sub
Public Sub StopProcess()
Dim RetVal As String
If Not p.HasExited Then
p.Kill()
End If
p.Close()
'p.Dispose()
End Sub
End Class


Nov 21 '05 #3

P: n/a
Hi,

AFAIK that shouldn't make a difference. All processes with the matching
name should get returned by Process.GetProcessesByName.

Regards,
-Adam.

Nikolay Petrov wrote:
thanks
I have a question
I am using my app to run instance of cmd.exe
what if there is another cmd.exe started outside of my process?
"Adam Goossens" <ad**********@users.sourceforge.net> wrote in message
news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi Nikolay,

Try Process.GetProcessesByName(). It will return to you a Process array of
all processes with the name you provide.

If the array is not empty, then you know the process has already been
started and you can take a reference to the old process rather than create
a new one.

---
Dim aProcesses() as Process
aProcesses = Process.GetProcessesByName("yourexecutable")

If aProcesses.Length > 0 Then
' process has been started before:
p = aProcesses(0)
Else
p = New Process
' initialize it here.
End If
----
Regards,
-Adam.

Nikolay Petrov wrote:
I have a class which starts a process and redirects it's output and
input.
My class have a method which starts the process and another which stops
it.
How can I check if the process have been already started, so I don't try
to start it again, and how to check if the process is not running so i
don't try to stop again?

code:
Public Class ProcEx
Private p As Process
Public Sub StartProcess()
p = New Process
With p.StartInfo
.FileName = "cmd"
.Arguments = ""
.CreateNoWindow = True
.ErrorDialog = False
.RedirectStandardError = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = Application.StartupPath
End With
p.Start()
End Sub
Public Sub StopProcess()
Dim RetVal As String
If Not p.HasExited Then
p.Kill()
End If
p.Close()
'p.Dispose()
End Sub
End Class


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.