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
publicfMain()
{
//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
}
}
相关推荐
基于winForm实现的C#女鞋销售系统.zip基于winForm实现的C#女鞋销售系统.zip基于winForm实现的C#女鞋销售系统.zip基于winForm实现的C#女鞋销售系统.zip基于winForm实现的C#女鞋销售系统.zip基于winForm实现的C#女鞋...
C#winform调用电脑摄像头
基于winForm实现的C#多线程大炮打飞机实验.zip基于winForm实现的C#多线程大炮打飞机实验.zip基于winForm实现的C#多线程大炮打飞机实验.zip基于winForm实现的C#多线程大炮打飞机实验.zip基于winForm实现的C#多线程...
C#编程 WinForm窗体开发 ChineseCode(源码)C#编程 WinForm窗体开发 ChineseCode(源码)C#编程 WinForm窗体开发 ChineseCode(源码)C#编程 WinForm窗体开发 ChineseCode(源码)C#编程 WinForm窗体开发 ChineseCode(源码)...
一款还不错的C#winform开发框架,对于想学习C#框架的初学者来说,是很值得借鉴的
c# winform实例大全(198个Csharp WinForm实例源码)
C# WinForm Application 写字板,服务控制器,定时关机,读写图片
经典的c#源代码,涉及面广,适合小白学习
经典C#WinForm小实例,真实代码。日常实用性较强,有需要欢迎关顾小店。
本系统使用winform的绑定机制应用以上模板及对应数据表(视图)实现动态代码生成后续需要做的工作就是应用winform开发自带的数据源方法或者devexpress的datalayoutcontrol方法来拖拽对应的控件和展示数据的gridcontrol...
winform(c#)最全60种非常漂亮界面样式\皮肤(经典)
C# Winform 发送邮件 C# Winform 发送邮件C# Winform 发送邮件
c#winform程序打包c#winform程序打包部署借鉴.pdf
C#俄罗斯方块(winform)C#俄罗斯方块(winform)C#俄罗斯方块(winform)C#俄罗斯方块(winform)C#俄罗斯方块(winform)C#俄罗斯方块(winform)C#俄罗斯方块(winform)
C# Winform 通用导航菜单C# Winform 通用导航菜单C# Winform 通用导航菜单C# Winform 通用导航菜单C# Winform 通用导航菜单C# Winform 通用导航菜单
C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码高亮的WinForm的TEXTBOX控件C#代码...
车票管理C# winform车票管理C# winform车票管理C# winform车票管理C# winform车票管理C# winform车票管理C# winform车票管理C# winform车票管理C# winform
C#_WinForm实现WebSocket及时通讯,客户端定向推送。
c# 基础教程winform 教程c# 基础教程winform 教程c# 基础教程winform 教程c# 基础教程winform 教程
C#编程 WinForm窗体开发 Selected(源码)C#编程 WinForm窗体开发 Selected(源码)C#编程 WinForm窗体开发 Selected(源码)C#编程 WinForm窗体开发 Selected(源码)C#编程 WinForm窗体开发 Selected(源码)C#编程 WinForm...