locked
Starting a process from a Windows Service RRS feed

  • Question

  • I have a service written in C#. It is installed and runs fine.

    Now I have written a new thread to control an external execution.

    Process starts but does not do any work.


                            ProcessStartInfo startInfo = new ProcessStartInfo();
    
                            startInfo.CreateNoWindow = false;
    
                            startInfo.UseShellExecute = true;
    
                            startInfo.FileName = Config.Get("Path") + strSeparator + "xyz.exe";
    
                            startInfo.WindowStyle = ProcessWindowStyle.Normal;
    
                            startInfo.Arguments = Config.Get("Parms") + Config.Get("InputPath") + strSeparator + strFile;
    
    
    
                            WriteToDebugFile("Invoke - " + startInfo.FileName + startInfo.Arguments);
    
    
    
                            int intMilsecs = 60*1000;
    
                            try
    
                            {
    
                                using (Process exeProcess = Process.Start(startInfo))
    
                                {
    
                                    exeProcess.WaitForExit(intMilsecs);
    
                                    if (exeProcess.HasExited)
    
                                    {
    
                                        WriteToDebugFile("Completed");
    
                                    }
    
                                    else
    
                                    {
    
                                        exeProcess.CloseMainWindow();
    
                                        exeProcess.Close();
    
                                     // exeProcess.Kill();
    
                                        WriteToDebugFile("Cancelled");
    
                                    }
    
                                }
    
                            }
    
                            catch (Exception exp)
    
                            {
    
                                WriteToDebugFile("Exception - " + exp);
    
                            }
    
    
    Invoke message comes out with command and parms looking great.

    After a minute wait, it flows to cancelled.

    I can see process start in task manger.

    It is still running after cancelled message.

    Kill raises exception of not active process.

    Program should process an input file and write an output file.

    Command Prompt execution of displayed command and parms works correctly.

    Thanks.
    • Edited by FredericLS Sunday, November 22, 2009 9:08 PM
    Sunday, November 22, 2009 1:31 AM

Answers

  • Hi FredericLS,

    Thanks for your clarification.

    I was unable to reproduce the issue by using the same code as yours to start a windows application process which doesn't show any UI.

    In order to troubleshoot this issue, can you add the following code at the start of Main method of the spawned process?

    System.Diagnostics.Debugger.Launch();
    In this way, windows will pop up a window to let you to choose a debugger to debug the process. You can step the code in VS debugger. Thanks.
     

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Hongye Sun - MSFT Thursday, December 17, 2009 6:50 AM
    • Marked as answer by Ji.Zhou Thursday, December 17, 2009 6:57 AM
    Wednesday, November 25, 2009 10:45 AM

All replies

  • Are you using Local System Account to run your service or others account? Does the account has sufficient right to your folder?

    MM


    chanmm
    Sunday, November 22, 2009 4:20 AM
  • Service and started task (I can see in task manager) are running as SYSTEM.

    Thanks for the reply.
    Sunday, November 22, 2009 5:58 AM
  • Based on several other threads, I have changed it to a specific UserID as there seems to be issues running under SYSTEM.

    Now both the Service and the started process are using the new ID.

    In order to avoid any issues with access rights, I have for now set the UserID to Admin group.

    The Service continues to function normally in all respects, including starting the new process, as it did before.

    Unfortunately, while the new process is running (as seen by Task Manager), it still does no work and no window.

    The CloseMainWindow and Close do not raise an exception, but the process continues to run.

    The Kill which is commented out does raise an exception as if the process was already gone, but the process is still in Task Manager.

    I seem to be getting nowhere.

    I had not previouly mentioned, but this is under Server 2008, in case there is an issue here that I was unaware.

    Any ideas on what to try next would be a big help!!

     
    Monday, November 23, 2009 6:20 AM
  • Hi FredericLS,

    Thanks for reporting this issue.

    If my understanding is correct, you are going to launch a windows application with UI from windows service in windows 2008. If I misunderstood you, please let me know.

    In Windows Vista and 2008, it changes windows service behavior by isolating session 0 and making it non-interactive. For detail, please read: http://msdn.microsoft.com/en-us/library/bb756986.aspx

    "The Windows Vista® and Windows Server® 2008 operating systems mitigate this security risk by isolating services in Session 0 and making Session 0 non-interactive. In Windows Vista and Windows Server 2008, only system processes and services run in Session 0. The first user logs on to Session 1, and subsequent users log on to subsequent sessions. This approach means that services never run in the same session as users' applications and are therefore protected from attacks that originate in application code."

    From the link, it provides following workarounds for your situation:

    • Use the WTSSendMessage function to create a simple message box on the user’s desktop. This allows the service to give the user a notification and request a simple response.

    • For more complex UI, use the CreateProcessAsUser function to create a process in the user's session.

    Hope it helps.

    Update: this article provides more detailed information about how to interact with users in windows service under windows Vista/2008:
    http://msdn.microsoft.com/en-us/library/ms683502(VS.85).aspx

     

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 23, 2009 9:27 AM
  • If my understanding is correct, you are going to launch a windows application with UI from windows service in windows 2008. If I misunderstood you, please let me know.


    Thanks for your reply, but You are incorrect.

    I am attempting to launch a Windows Application WITHOUT UI (This is usually called batch processing - Application reads a file, writes an XML file and a log) from a Windows Service under Windows Server 2008. I believe it may also be possible to redirect std input, output and error, but I do not need this for now.

    Initially I did try to run this as a SYSTEM service and the started process also as a SYSTEM task. I checked the service as "allow service to interact with desktop". Task Manager showed both as owned by SYSTEM.

    In all cases (before as SYSTEM and now as USER with ADMIN rights), the spawned process starts but does not read, write, log, or even termanate. I must manually cancel the process with Task Manager to get it to actually stop and go away.

    The service and its eight threads all work correctly, except this new thread, and continues to work even after I cancel the stalled spawned process.

    My service does all its work perfectly, I just can not get it to run an external batch process.

    If I bring up a command prompt and run the batch process manually, the service then processes the batch output and all I want to do is done.

    Thanks for your attention.

    Tuesday, November 24, 2009 1:09 AM
  • Hi FredericLS,

    Thanks for your clarification.

    I was unable to reproduce the issue by using the same code as yours to start a windows application process which doesn't show any UI.

    In order to troubleshoot this issue, can you add the following code at the start of Main method of the spawned process?

    System.Diagnostics.Debugger.Launch();
    In this way, windows will pop up a window to let you to choose a debugger to debug the process. You can step the code in VS debugger. Thanks.
     

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Hongye Sun - MSFT Thursday, December 17, 2009 6:50 AM
    • Marked as answer by Ji.Zhou Thursday, December 17, 2009 6:57 AM
    Wednesday, November 25, 2009 10:45 AM
  • Hi FredericLS,

    Have you tried my suggestion and if it works? Please let me know if the issue is still not resolved. Thanks.

     

    Hongye Sun [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, December 10, 2009 3:19 AM