stop\kill process

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
Hi all,

How can i stop or kill a process which i see in the task manager?

for an example i have 2 process of Excel.exe how can i stop or kill them by process name?

Thanks in advanve:)
 
My second thought would be why do you have two Excel's open and why are you trying to kill them.

I've seen similar approaches where Excel app's wouldn't release their instances on closing, but its far better to fix the code causing them to remain open than to randomly kill rogue processes.
 
i think you'll find this of value:
VB.NET:
    Private Sub KillProcess(ByVal ProcessName As String)
        Dim prc() As Process
        Dim ERROR_FILE_NOT_FOUND As Integer = 2
        Dim ERROR_ACCESS_DENIED As Integer = 5
        Try
            prc = System.Diagnostics.Process.GetProcessesByName(ProcessName)
            Dim eprc As IEnumerator = prc.GetEnumerator
            eprc.Reset()

            While eprc.MoveNext
                Dim proc As Process = CType(eprc.Current, Process)
                proc.Kill()
                proc = Nothing
            End While

            eprc = Nothing
            prc = Nothing

        Catch e As System.ComponentModel.Win32Exception
            If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
                'MessageBox.Show(e.Message + ". Process not found.")
                Throw New UnauthorizedAccessException(e.Message & ". Process not found.")
            Else
                If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
                    ' Note that if your word processor might generate exceptions 
                    ' such as this, which are handled first. 
                    'MessageBox.Show(e.Message + ". You do not have permission to kill this process.")
                    Throw New UnauthorizedAccessException(e.Message & ". You do not have permission to kill this process.")
                End If
            End If
        End Try
    End Sub
 
Yea, Juggalo has the code there to take care of it for you. I would still encourge you to evaluate its usage before accepting the approach.

I say this really because the exe you mention is Excel, and I've seen numerous times where programmers have Excel remain open because they were using something in a spreadsheet and did an Application.Quit that didn't end the process. There is almost always a reason for this, and I'd highly suggest fixing that rather than just prockills.

All that said, I could be wrong, you could just be wanting to play with some cool stuff like process handles =)
 
Application.Quit will end the instance of the currently running excel app however to properly release all the resources used by that object you should make a call to


VB.NET:
Marshall.ReleaseComObject(..)
 
Ok, this is the code i'm using in order to get info from the excel sheet.

VB.NET:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
[COLOR=green]'Reading and getting info from excel sheet[/COLOR]
Dim row As Int32
Dim cellref, val As String
[COLOR=green]'create excel application and hide it[/COLOR]
Dim ex As Application = New ApplicationClass()
ex.Visible = False
[COLOR=green]'open the workbook[/COLOR]
Dim wb As Workbook = ex.Workbooks.Open("c:\CalcUpDate\yazdayrr.xls")
If wb IsNot Nothing Then
[COLOR=green]'default worksheet is item 1[/COLOR]
Dim ws As Worksheet = DirectCast(wb.Worksheets(1), Worksheet)
Dim UPdate As Date
[COLOR=green]'get date of update[/COLOR]
cellref = Chr(68) & (4)
UPdate = DirectCast(ws.Range(cellref), Range).Value
MsgBox(UPdate)    [COLOR=green] 'for test only[/COLOR]
[COLOR=green]'loop thru as many rows/cols as you need[/COLOR]
For row = 0 To 13
[COLOR=green]'build cell reference ie. 0,0 = A1[/COLOR]
cellref = Chr(68) & (row + 6)
[COLOR=green]'value from cell range[/COLOR]
val = DirectCast(ws.Range(cellref), Range).Value
 
currAry(row) = val
MsgBox(currAry(row), MsgBoxStyle.OkOnly) [COLOR=green]'for test only[/COLOR]
Next
End If
 
End Sub

This code opens an Excel process and when i'm ending the debug i can see the the process stays in the Task Manager, and that is why i was looking for a way to kill it.

Now, my question is, do i need all the code that JuggaloBrotha wrote down in order to kill one process??
 
Don't use ApplicationClass, use Excel.Application instead. (Dim x As New Excel.Application), and when you are finished with it you quit the application (x.Quit).
 
I just read your post JohnH and i thought, he's just written exactly what i have written. Then i re-read my own post, i ment to say ExcelApplication.Quit. Just goes to show what happens when you post a reply with a donut in your mouth.:)
 
well, i have changed to excel.application

VB.NET:
Dim ex As Application = New Excel.Application

and add ex.quit()

and unfortunately it doesn't work.

I'm so frustrated :(
 
Unfortunately, still not working....

that's what i did:

VB.NET:
System.Runtime.InteropServices.Marshall.ReleaseComObject(ex)
 
Back
Top