Killing a Process if no App.

M

Miro

I'm using VB.Net 2003

I have code - it works great - to kill a process(s) if they are running.
----
Dim myProcesses() As Process 'Funny - withouth the () in the
myProcesses it does not work
Dim myProcess As Process

myProcesses = Process.GetProcessesByName(Trim(TextBox1.Text))
For Each myProcess In myProcesses
myProcess.Kill()
Next

Close() 'Program stops running after it is run.
----

The user has a program that runs, but when the program loads, sometimes it
loads in the
"Processes" tab, but not in the Application tab ( of the Windows Task
Manager ). This program
loads up some files associated with it, and actaully Locks these files until
its killed. This program also
only allows you to run one instance of it.

So what I would like to do, is Get all the processes ( see above code ), and
see if there is an Application
running associated with it.
If there is not, then kill that process.
If there is an Application, then just set focus to it.

I cannot find help on this on the net.
Can someone point me in the right direction please.

Thanks,

Miro
 
G

Guest

The reason that you need the () after myprocesses is that it is used as an
array when getting all the names of the processes, i.e., GetProcessesByName
returns an array all processes with the name passed to the method.
 
M

Miro

Thanks that answers that little if part. I was gonna search for that later.
I originally got this example from a website ( im still learning vb ) and it
didnt have it.
So i couldnt figure out why i needed it and they didnt.

Does anyone know how to do the App and process thing?

Thanks again Dennis.

Miro
 
M

Miro

Also one more question i just ran accross this.

What is the difference between:
Dim myProcesses() As Process
and
Dim myProcesses() As System.Diagnostics.Process

I find the System.Diagnostics works better because then i can do this:

If myProcesses.Length > 0 Then

If I just declare it as a Process I cannot get the length of it.
 
G

Guest

Both of the below work. The System.Diagnostics is just the namespace which
contains the Process Class and you are just using the fully qualifiec Class
Type Name when you dimension Process using the System.Diagnostics.

Dim myprocesses() As Process
If myprocesses.Length > 0 Then
'do something
End If

Dim myProcesses() As System.Diagnostics.Process
If myprocesses.Length > 0 Then
'do something
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to identify your own processes 4
Closint processes 2
process and Listbox 4
Uniquely identify a running exe? 1
Process CPU Usage 1
automation program closing ? 2
Start In & Processes 2
Process 3

Top