Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I would like to ask if there any other methods to kill a process using vb.net?
I have tried the process.Kill() method and it is working fine in my local but upon testing it in server, it isn't working and the processes remains in background.
Do you have any suggestion?

What I have tried:

VB
Public Sub CreateNew_Word(ByVal strResult As List(Of String))

'Added Code
'Description:Get all running winword processes then stored it in the list
        Dim processIds As New List(Of Integer)()
       For Each process__1 As Process In Process.GetProcessesByName("winword")
            processIds.Add(process__1.Id)
        Next

'***WORD FILE GENERATION HERE****

'Remove clean-up code to test the killing of process
'ReleaseObject(rng)
'newDoc.Close(False)
'ReleaseObject(newDoc)

'WordApp.Application.Quit(False)
'ReleaseObject(WordApp)

'GC.Collect()
'GC.WaitForPendingFinalizers()
'GC.Collect()
'GC.WaitForPendingFinalizers()

'Description: As the existing processes has been stored in the list. Those processes will not be killed. Only the generated winword.exe will be killed because it is not inside the list.
        For Each process__1 As Process In Process.GetProcessesByName("winword")
            If Not process__1.HasExited AndAlso Not processIds.Contains(process__1.Id) Then
                process__1.Kill()
            End If
        Next
Posted
Updated 31-May-17 22:14pm
Comments
Suvendu Shekhar Giri 31-May-17 22:07pm    
You are killing a process via ASP.Net?
What is the purpose?
Aifos Lavigne 31-May-17 23:21pm    
The winword.exe remains in background processes after generating the word file. I tried several cleanup code which is working fine in my local but not in server. So I come up to kill the process instead to be able to terminate its background processes. But still not working. The service account using is an admin account but still I cannot be able to figure the out the problem.
Richard Deeming 1-Jun-17 9:36am    
REPOST
You already posted this question last week.
https://www.codeproject.com/Questions/1189039/Closing-of-word-process-in-the-server-using-VB-NET[^]

The answer has not changed - automating Office from ASP.NET is not supported. Use the OpenXML SDK[^] or DocX[^] to create or modify Word documents.

If this is an ASP.NET or a Windows Service application you can NOT use Word. None of the Office applications are supported in a non-user interactive environment.
 
Share this answer
 
Comments
Richard Deeming 1-Jun-17 9:34am    
I've countered one of the down-votes, but it looks like there are at least two people who've never read KB257757!

Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
Dave Kreskowiak 1-Jun-17 9:53am    
On top of that he said he was going to try DocX and apparently never did, because...reasons.
When you run the code on your local machine it is probably running under your own account so it has the rights to kill processes. On the server it is running under an account that doesn't have these permissions. Also Word is not supported for asp.net automation which is why you're seeing the problems you are with processes hanging around causing problems.

You have already been told this isn't going to work (now you're finding out why) and to use some other method of managing Word documents.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900