Run Windows Service as Winform application in C#

Introduction

This article shows how Windows Service can be run as Windows application with UI when the application is build in Debug.

Why should we start Windows Service as Winform application at first place?
Windows Services do not have user interface and usually you do not need to be run them as Windows application. Unfortunately, to debug a windows service application you have to register it, start it from Services and then attach to the process. To avoid this lengthy process for testing and debugging purposes I use to run the service as Windows application. In this way I do not have to attach to service process in order to debug it. Also, I can make to start and stop the service from the Winform user interface.

How does it work?

In order Winform service interface to be available only when it is debugging I use #if preprocessor directive to check if the application is build in Debug or Release mode. When it is build debug version it is created winform which create instance of the service class (This is done in the starting point of the application - Main method).

Also for the debug version is provided two additional methods in the Service class (called StartService and StopService) through which the service can be started or stopped.

In order to create winform in your service application you should include reference for System.Windows.Forms, System.Drawing and System

The entire C# Code:

Program.cs

using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;

#if DEBUG
//For debug use Winform
using System.Windows.Forms;
using System.Drawing;
using System;
#endif


namespace TestWindowsService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
#if DEBUG
//If Debug start winform UI
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new fMain());

#else
//Else run as Service
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new TestService() };
ServiceBase.Run(ServicesToRun);

#endif
}
}
#if DEBUG
public classfMain Form
{
//Field for Testing the Windows Service
private TestService _testService = null;

#region [User Interface members]

private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Button bStartService;
private System.Windows.Forms.Button bStopService;

#endregion

public 
fMain()
{
//Initialize Form Components
InitializeComponent();

//Create Service Instance
_testService = new TestService();
}

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.bStartService = new System.Windows.Forms.Button();
this.bStopService = new System.Windows.Forms.Button();

//Start Service Button
this.bStartService.Location = new System.Drawing.Point(9, 9);
this.bStartService.Name = "bStartService";
this.bStartService.Size = new System.Drawing.Size(100, 23);
this.bStartService.TabIndex = 1;
this.bStartService.Text = "Start Service";
this.bStartService.UseVisualStyleBackColor = true;
this.bStartService.Click += newSystem.EventHandler(this.bStartService_Click);

//Stop Service Button
this.bStopService.Location = new System.Drawing.Point(9, 40);
this.bStopService.Name = "bStopService";
this.bStopService.Size = new System.Drawing.Size(100, 23);
this.bStopService.TabIndex = 1;
this.bStopService.Text = "Stop Service";
this.bStopService.UseVisualStyleBackColor = true;
this.bStopService.Click += newSystem.EventHandler(this.bStopService_Click);

//fMain form Settings
this.Text = "TestService";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(120, 90);
this.Controls.Add(this.bStartService);
this.Controls.Add(this.bStopService);
this.ResumeLayout(false);
this.PerformLayout();
}

private void bStartService_Click(object sender, EventArgs e)
{

//Start the service from the form
_testService.StartService();
}

private void bStopService_Click(object sender, EventArgs e)
{
//Stop the service from the form
_testService.StopService();
}
}
#endif
}

TestService.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;

namespace TestWindowsService
{
public partial class TestService : ServiceBase
{
public TestService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
}

protected overridevoid OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}

#region Windows test interface methods

#if DEBUG
/// <summary>
/// Winform UI function for starting the service
/// </summary>
internal void StartService()
{
OnStart(null);
}

/// <summary>
/// Winform UI function for stopping the service
/// </summary>
internal void StopService()
{
OnStop();
}
#endif

#endregion
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值