Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

process.kill in asp.net is access denied

Posted on 2009-07-08
5
Medium Priority
?
2,807 Views
Last Modified: 2012-06-27
In the following code, this is my excel process kill for clearing memory after use by asp.net. It has worked before I change my IP address, after that there're some error about "System.ComponentModel.Win32Exception: Access is denied" at proc.Kill(). When I change my IP to the old one, it work, but I have to using new IP. So please give me some idea about this.
For Each proc As Process In Process.GetProcessesByName("EXCEL")
     proc.Kill()
 Next

Open in new window

0
Comment
Question by:KrichayaE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:ppittle
ID: 24804305
From the Process.Kill documentation http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(VS.80).aspx:

If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

Try using proc.WaitForExit() http://msdn.microsoft.com/en-us/library/ty0d8k56(VS.80).aspx.  This does a synchronus close, meaning your program will halt execution until the Excel process has closed.  The Kill method is aynschronus, meaning that your code will not wait for Excel to close.  If the Kill method is called for the same process more than once, an Exception will be thrown.

Also, the Execution Context at the time when proc.Kill or proc.WatForExit() executes needs to have full trust permission.  One way to do this is with WindowsImpersonationContext:

using (WindowsImpersonationContext impCtx = (HttpContext.Current.User.Identity as WindowsIdentity).Impersonate())
{
  //Kill process code
 
  impCtx.Undo();
}

0
 

Author Comment

by:KrichayaE
ID: 24810393
I found my jerky cause, it come from Excel File opening from outside asp.net. So "Process.GetProcessesByName("EXCEL")" is include my Excel File into process and surely that it will access denied.

In the other way, I try to using "proc.WaitForExit()" like ppittle suggest. It will waiting until timeout if I not closing this Excel File.

So I have an idea that "Process.GetProcessesByName("EXCEL")" should get process by user name but I don't know how to. Are there any way that "Process.GetProcessesByName("EXCEL")" will check process from user "ASPNET" only ?
0
 
LVL 8

Accepted Solution

by:
ppittle earned 2000 total points
ID: 24842156
Give this a try:

//Iterate through all Excel processes on local machine
foreach (Process proc in Process.GetP{rocessesByName("EXCEL"))
{
   //Only work with Processes started by ASPNET
   if (proc.StartInfo.UserName.Equals("ASPNET", StringComparison.InvariantCultureIgnoreCase))
   {
            //Impersonate a user that has permissions to kill the process
            using (WindowsImpersonationContext impCtx = (HttpContext.Current.User.Identity as    WindowsIdentity).Impersonate())
{
  //Kill process code
 
  impCtx.Undo();
}

    }
}
0
 

Author Comment

by:KrichayaE
ID: 24855912
Yah! It work. Thanks a million, ppittle.
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24859935
I'm glad to hear the solution worked for you.  Happy coding!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question