Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a console application to close w3wp exe , below is the code.
C#
public static void KillProcessByNameAndUser(string ProcessName, string ProcessUserName, int HasStartedForHours)
       {
           Process[] foundProcesses = Process.GetProcessesByName(ProcessName);
          
           foreach (Process p in foundProcesses)
           {         
               p.Kill();           
           }
        }

when I run it as a normal exe by clicking it working final, but when i call it in my asp.net page it also close the w3wp exe, but it also stopping the IIS application pool. I need only to stop the w3wp.exe not the IIS application pool. This is how I called it in my asp.net page.
C#
Process p = new Process();
            p.StartInfo.FileName = @"C:\inetpub\wwwroot\Kill\Debug\Kill.exe";
            p.Start();

Please help asap why is that happening and any solution for this.
Posted
Comments
F-ES Sitecore 15-Sep-15 9:22am    
I think this is one of those situations where you should explain the problem you're trying to solve rather than stating why your chosen solution doesn't work, as there might be a batter way of doing things.
Dipayan Basu 15-Sep-15 9:25am    
Actually I am trying to close all opened w3wp.exe and excel.exe after automatic scheduled execution of websites.
Dave Kreskowiak 15-Sep-15 9:30am    
WHY??

Do you realize that w3wp.exe is what is running you website? If you stop that process OF COURSE it's going to kill your application pool.

Again, WHY do you think you need to do this? WHAT "automatic scheduled execution of websites"? What does "execution of websites" even mean?
F-ES Sitecore 15-Sep-15 9:31am    
Automating Excel from asp.net isn't supported. You should use a method of manipulating Excel files that is such as the odbc driver, Microsoft's XML SDK, or third party libraries like aspose, docx etc. That way you won't need to kill your processes to release copies of Excel.
Dipayan Basu 15-Sep-15 10:08am    
We have previously coded projects, so we can't change in them, what we have to do is closing the running exe's , we don't having problems in closing the excel exe, we are facing problem in closing the w3wp exe. as it stopping the iis application pool.

1 solution

you have to send excel a "WindowClose" message in Sequence to forcefully close the process.

You will be use these API functions
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

And it should be look something like:
// First, Find handle 
hWindow = FindWindow(vbNullString, "Excel")
//Find proccess ID
GetWindowThreadProcessId(hWindow, ProcessValueID)
//Kill the process
ProcessValue = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), ProcessValueID)
TerminateProcess(ProcessValue, CLng(0))
CloseHandle ProcessValueID
 
Share this answer
 
v2

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