0

I've created a simple socket application in which I have to open a socket receiver to listen for incoming messages on desktop. Problem is I need to be able to run the application as background service and I need to be able to start and stop the service from System Tray icon, for that I've created a windows forms application and added and I've created a windows service inside a windows forms application. On debug mode when I call the service code directly It's working fine. The service is installing and the OnStart method is being called. But when I try to start the application after building the project and running the .exe file I get the following error.

"Cannot start service from the command line or a debugger a windows service must first be installed. Then start with the server explorer"

And when I try to start the service manually from the windows services panel I get the following error.

"Error 1053: The service did not respond to the start or control request in a timely fashion".

This is the code used to install the service which is working fine.

AssemblyInstaller installer = GetInstaller(assembly)
installer.Install(state);
installer.Commit(state);

And this is the code for starting the service which is giving the error. "Cannot start service from the command line or a debugger a windows service must first be installed. Then start with the server explorer"

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new SocketService()
        };
        ServiceBase.Run(ServicesToRun);

Can anyone please help me with this. I need to create a single msi installation utility of the entire application. In which I need to install both the desktop application and the windows service. Where the desktop application can Start or Stop the service. Thanks.

  • 1
    You can't embed a windows service into a WinForms app. 1) WinForms apps don't expose the mechanisms for Windows Service Manager to be able to interact with it hence the _"timely fashion"_ message 2) WinForms apps have a different startup method signature. 3) WinForms apps are by definition GUI apps and Windows Services by default cannot have a GUI. The only reason your code is working in debug mode is because you are calling the service implemention directly. I recommend splitting into two processes, one the WinForms GUI & the other the Windows Service. You can make them talk to each other –  Oct 30 '20 at 22:47
  • 1
    ...also Windows Services can run _before_ a user has logged in as they don't require an interactive user or UI. WinForms do –  Oct 30 '20 at 23:26
  • These articles may be helpful. [Application Compatibility - Session 0 Isolation](https://techcommunity.microsoft.com/t5/ask-the-performance-team/application-compatibility-session-0-isolation/ba-p/372361), [TCP IP Listener in windows Service](https://stackoverflow.com/q/39097719/9014308), [Starting GUI application from Windows Service - Approaches](https://stackoverflow.com/q/61901844/9014308) – kunif Oct 31 '20 at 07:37
  • Thanks for your help. Can you please tell me what will be the best possible solution to create a system tray application which can start and stop a windows service ? – Nasir Ikram Oct 31 '20 at 23:35

0 Answers0