Killing a Process if no App.

  • Thread starter Thread starter Miro
  • Start date Start date
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
 
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.
 
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
 
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.
 
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
 
Back
Top