question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked RLWA32-6355 commented

Windows Service and Destop

Hi All,
I need to run my program in windows service, it possible?.I test a lot of code but fail in OnStart method.
somebody can show the simple code.

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Diagnostics;
 using System.Linq;
 using System.ServiceProcess;
 using System.Text;
 using System.Threading.Tasks;
    
 namespace Win_SerV
 {
     public partial class TesT_Service : ServiceBase
     {
         public TesT_Service()
         {
             InitializeComponent();
         }
    
         protected override void OnStart(string[] args)
         {
             try
             {
                 using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                 {
                     process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"C:\\Users\\family\\Desktop\\_Exe_6.exe");
                     process.StartInfo.CreateNoWindow = true;
                     process.StartInfo.ErrorDialog = false;
                     process.StartInfo.RedirectStandardError = true;
                     process.StartInfo.RedirectStandardInput = true;
                     process.StartInfo.RedirectStandardOutput = true;
                     process.StartInfo.UseShellExecute = false;
                     process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                     process.Start();
                 }
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
             }
         }
    
         protected override void OnStop()
         {
    
         }
    
    
     }
 }
dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

You will have to learn Windows session isolation before attempting to write such an application, https://techcommunity.microsoft.com/t5/ask-the-performance-team/application-compatibility-session-0-isolation/ba-p/372361 The Windows service app runs in session 0 won't be able to access easily resources in a user session.

0 Votes 0 ·

Thank Lextm your Link.

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered RLWA32-6355 commented

Is there an exception in the program? Or is it just that the call was not successful, and nothing happened?

It is not a good way to call other applications like this in Windows service, especially those GUI programs.

Such an application requires a user interface to start. But Windows Service can run without a desktop session.

If you just need to start a program, you can consider creating a new Task in Task Schedule.

121559-capture.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


capture.png (38.9 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank for your feeback TimonYang,
( Is there an exception in the program? Or is it just that the call was not successful, and nothing happened?)
= nothing happened.

Create and Install the windows service are succesfull.

0 Votes 0 ·

Since session 0 is a non-interactive session any UI created by a process started in that session will not appear. Also, writing to a console to display information about an Exception would not be visible. A service can write informational and error messages to the event log.

So if your service was actually successful in starting a process from the user's perspective it would still appear that "nothing happened".

Use Task Manager to see if your service actually started a process for the targeted executable (_Exe_6.exe).

0 Votes 0 ·

Thank you RLWA32 for your info.

0 Votes 0 ·
Show more comments

"But Windows Service can run without a desktop session" is incorrect. Session 0 has its own desktop, https://techcommunity.microsoft.com/t5/ask-the-performance-team/application-compatibility-session-0-isolation/ba-p/372361

0 Votes 0 ·