Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my Windows Service project, the project looks to be started but actually not. A piece of related code is below:
C#
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {  new Service1() };
g.Write2Log(g.logFolder, " WinService - PhotoFile: ServicesToRun before start  " + DateTime.Now);
try {
    ServiceBase.Run(ServicesToRun);
    g.Write2Log(g.logFolder, " WinService - PhotoFile: ServicesToRun after start  " + DateTime.Now);
} catch (Exception ex) {
    g.Write2Log(g.logFolder, "ServicesToRun can't be launched - Exception: " + ex.Message + "  " + DateTime.Now);
}

In my log file, the "ServicesToRun before start" message displays but the
"ServicesToRun after start" never displays. It means the
ServicesToRun = new ServiceBase[] { new Service1() }
is actually not called, on OnStart() event is not fired.
Referring to c# - Allow service to interact with desktop in Windows - Stack Overflow[^]
I checked on "Allow service to interact with desktop", but the problem still exists. What else should do for this bug? Thanks.

What I have tried:

Window Service actually not started properly
Posted
Updated 8-Aug-19 5:42am
v3
Comments
ZurdoDev 6-Aug-19 11:53am    
Check event viewer.
s yu 6-Aug-19 12:48pm    
The message in the Event View is "Service started successfully." Nothing else.

The article you are referring too is too old, from 9 years ago. On Windows XP this would work but newer Windows versions simply do not allow Desktop interaction anymore.
 
Share this answer
 
Your service has started properly, and there is no bug.

The ServiceBase.Run method does not return until after the service stops. Your "after start" message won't be logged until the service has stopped.

If you want to log when the service has started, add the code to your service's OnStart method.
 
Share this answer
 

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