`
lovnet
  • 浏览: 6711474 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

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#ifpreprocessor 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

usingSystem.Collections.Generic;
usingSystem.ServiceProcess;
usingSystem.Text;

#ifDEBUG
//For debug use Winform
usingSystem.Windows.Forms;
usingSystem.Drawing;
usingSystem;
#endif


namespaceTestWindowsService
{
static classProgram
{
/// <summary>
///The main entry point for the application.
/// </summary>
static voidMain()
{
#ifDEBUG
//If Debug start winform UI
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(newfMain());

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

#endif
}
}
#ifDEBUG
public classfMain:Form
{
//Field for Testing the Windows Service
privateTestService_testService =null;

#region[User Interface members]

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

#endregion

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

//Create Service Instance
_testService =newTestService();
}

private voidInitializeComponent()
{
this.components =newSystem.ComponentModel.Container();
this.bStartService =newSystem.Windows.Forms.Button();
this.bStopService =newSystem.Windows.Forms.Button();

//Start Service Button
this.bStartService.Location =newSystem.Drawing.Point(9, 9);
this.bStartService.Name = "bStartService";
this.bStartService.Size =newSystem.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 =newSystem.Drawing.Point(9, 40);
this.bStopService.Name = "bStopService";
this.bStopService.Size =newSystem.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 =newSystem.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 voidbStartService_Click(objectsender,EventArgse)
{

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

private voidbStopService_Click(objectsender,EventArgse)
{
//Stop the service from the form
_testService.StopService();
}
}
#endif
}

TestService.cs

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Diagnostics;
usingSystem.ServiceProcess;
usingSystem.Text;

namespaceTestWindowsService
{
public partial classTestService : ServiceBase
{
publicTestService()
{
InitializeComponent();
}

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

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

#regionWindows test interface methods

#ifDEBUG
/// <summary>
///Winform UI function for starting the service
/// </summary>
internal voidStartService()
{
OnStart(null);
}

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

#endregion
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics