-2

I developed c# a GUI form with only "Start" and "Stop" something like:

private void btnStart_Click(object sender, EventArgs e)
{
 //Some code here
}

private void btnStop_Click(object sender, EventArgs e)
{
//Some code here
}

I need to run this app as service.

How can I run my GUI app as a service? plz help

Error 404
  • 427
  • 5
  • 25
  • 1
    GUI projects don't run as Windows Services. – gunr2171 Dec 14 '17 at 17:19
  • You want to run it as a Windows service? The one that you see in services.msc? Then you need to create a Windows service app- http://www.c-sharpcorner.com/UploadFile/b7531b/create-simple-window-service-and-setup-project-with-installa/ – Souvik Ghosh Dec 14 '17 at 17:19
  • @gunr2171 WCF Service is not a windows service. – Logman Dec 14 '17 at 17:20
  • @gunr2171 How can I "Convert" my app to service app? – Error 404 Dec 14 '17 at 17:20
  • @Error404 please clarify WCF Service or Windows service? – Logman Dec 14 '17 at 17:20
  • "WCF itself is a distributed communication platform like .net remoting, webservice, COM+ which is not designed for providing GUI functionality. However, WCF service can be hosted in various host applications such as winform, WPF, ASP.NET" https://social.msdn.microsoft.com/Forums/vstudio/en-US/1db04026-6ee1-4256-b2fc-ee9052e82801/can-wcf-service-host-a-gui-control?forum=wcf – gunr2171 Dec 14 '17 at 17:25
  • @Logman I think windows service, they just asked me "Run it as a service", I don't know if there a difference between wcf service or just "service". I think that something that not interact with the user as Jim Mischel said – Error 404 Dec 14 '17 at 17:26
  • As gunr2171 mention above WCF Service is a tool for communication between multiple applications like for ex. desktop app and web server. It may be hosted inside another application like IIS server. If it's not what you want you probably should remove section about it from your question as it is misleading. – Logman Dec 14 '17 at 17:34
  • @Logman I removed the wcf part – Error 404 Dec 14 '17 at 17:37
  • And for creating windows service there is an example article from MS https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer#BK_CreateProject – Logman Dec 14 '17 at 17:38
  • @Logman Thank you – Error 404 Dec 14 '17 at 17:43

2 Answers2

2

The best way is to put the non-UI classes and logic in its own assembly (or assemblies), then have both a winform project and a windows service project that reference that assembly.

Technically you could call winform code from within a service assembly, but it would be messy, and debugging is much more difficult.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Do you know how to stop the service properly? I mean, what shoul I put in the stop button, something like `protected override void OnStop(){ Service.Exit}` – Error 404 Dec 14 '17 at 19:16
  • 1
    There are no UI components in a service, so there is no stop button. If you want the service to stop, you stop it through the services console (`start --> run --> services.msc`) or by running the `sc` command (type `sc /?` in a command window for details). If you have things that you need to do when the stop command is issued, put that code in the `OnStop` event. – Rufus L Dec 14 '17 at 20:34
1

The reason why the program registered as a service can not display the GUI is due to specification change called session 0 isolation.

Please refer to this article and change the program.
Service starting a process wont show GUI C#

kunif
  • 4,060
  • 2
  • 10
  • 30