473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Process question

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
3 1254
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael J. Moore | last post by:
Is it the listener process, or some other Oracle process. Also, on a UNIX system, when you do "ps -ef" to see your processes, the PPID points back to a process named "init". Why does the PPID not...
2
by: mwazir | last post by:
Hi all, I have reposted this question from dotnet.general as I have been advised that this is a more appropriate forum for this question. Apologies for the repost. I have a process thats...
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
6
by: A | last post by:
Hi all, This is sort of a newbie question. I have a method that creates a process which in turn installs MSDE. Basically, after I start the process I need to wait until it is complete and then...
2
by: ca___t | last post by:
Hi there : I want to start a consoneApplication using process.start method at web application server side.I have a question at this.Why the consoneApplication's process is start but not run...
22
by: Zen | last post by:
Hi, My production machine has 2G of memory, when aspnet_wp.exe goes up to about ~1.2G of memory usage, I start get out-of-memory exception. Other processes don't use as much memory and I added...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
10
by: Susan | last post by:
I have a process that takes a while to run so I have it running asynchronously so that the user can continue working. My question is that I want to killl the process if the user changes the search...
9
by: SeC | last post by:
Hi. Is there any way to detect if application is being killed by 'End Process' via Task Manager ?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.