locked
Close process RRS feed

  • Question

  • Hi all,
    I have a problem. I have a WCF service (.NET 4.0, running on Microsoft Server 2003, IIS 6) that start application (notepad.exe). Code below:

    Process process = new Process();
    process.StartInfo.FileName = "C:\WINDOWS\notepad.exe";
    process.StartInfo.Arguments = "C:\Work\Text.txt";
    startProcess = process.Start();
    
    

    When I call service,  in Task Manager (on server) I see in Processes tab notepad.exe, user name NETWORK SERVICE. When I try:

    process.CloseMainWindow();
    
    it returns false and process notepad.exe stay running and process.MainWindowHandle.ToString() returns 0. The process.Kill() kills the process, butit it is not good solving.

    Please, can anybody help me, how close it normally not process.Kill()?

    Thanks,
    Martin


    MK
    Tuesday, December 14, 2010 2:27 PM

Answers

  • You're starting a process from a service and that process has a UI.  Very bad idea.  It won't even work properly under newer OSes.  If this is just for a test then fine but services should not start any process that requires any sort of UI because they won't work properly. 

    In your specific case the IIS service is starting a new process under its own account (NETWORK SERVICE) and that is the desktop under which the UI will run.  No other user will be able to access or close that window (at least in newer OSes).  CloseMainWindow just sends a close request to the app.  What I'm not sure about on Server 2003 is whether the newer security constraints were put in yet or not.  On newer OSes you can't send messages to processes outside your user account.  If you were to call process.Start, wait for the window to appear and then call CloseMainWindow (from the same block of code) then it might work in 2003 but given that the window handle is 0 you either don't have visibility to the window (because of security) or the window was not open yet.  Start() does not wait for the UI to appear it simply starts the process and returns.

    Michael Taylor - 12/14/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by Leo Liu - MSFT Tuesday, December 21, 2010 9:18 AM
    Tuesday, December 14, 2010 2:35 PM
  • We can use the below code for kill the process

    using System.Diagnostics;

    foreach (Process proc in Process.GetProcesses())
    {
       if (proc.ProcessName.StartsWith("notepad"))
       {
           proc.Kill();   
       }
    }

    I hope this will helpful.

     


    With Regards, DP
    • Proposed as answer by DP_CHE Tuesday, December 14, 2010 3:26 PM
    • Marked as answer by Leo Liu - MSFT Tuesday, December 21, 2010 9:18 AM
    Tuesday, December 14, 2010 3:25 PM

All replies

  • You're starting a process from a service and that process has a UI.  Very bad idea.  It won't even work properly under newer OSes.  If this is just for a test then fine but services should not start any process that requires any sort of UI because they won't work properly. 

    In your specific case the IIS service is starting a new process under its own account (NETWORK SERVICE) and that is the desktop under which the UI will run.  No other user will be able to access or close that window (at least in newer OSes).  CloseMainWindow just sends a close request to the app.  What I'm not sure about on Server 2003 is whether the newer security constraints were put in yet or not.  On newer OSes you can't send messages to processes outside your user account.  If you were to call process.Start, wait for the window to appear and then call CloseMainWindow (from the same block of code) then it might work in 2003 but given that the window handle is 0 you either don't have visibility to the window (because of security) or the window was not open yet.  Start() does not wait for the UI to appear it simply starts the process and returns.

    Michael Taylor - 12/14/2010
    http://msmvps.com/blogs/p3net

    • Marked as answer by Leo Liu - MSFT Tuesday, December 21, 2010 9:18 AM
    Tuesday, December 14, 2010 2:35 PM
  • We can use the below code for kill the process

    using System.Diagnostics;

    foreach (Process proc in Process.GetProcesses())
    {
       if (proc.ProcessName.StartsWith("notepad"))
       {
           proc.Kill();   
       }
    }

    I hope this will helpful.

     


    With Regards, DP
    • Proposed as answer by DP_CHE Tuesday, December 14, 2010 3:26 PM
    • Marked as answer by Leo Liu - MSFT Tuesday, December 21, 2010 9:18 AM
    Tuesday, December 14, 2010 3:25 PM
  • OK,

    thank you very much.

    Martin


    MK
    Wednesday, December 15, 2010 1:42 PM