调试windows服务最简单的方法之一
先看一下这段启动代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text; namespace WindowsService1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
Service1 s = new Service1(); if (Environment.UserInteractive)
{
s.DebugStart(); Console.ReadKey(); s.DebugStop();
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { s };
ServiceBase.Run(ServicesToRun);
}
}
}
}
关键就在对Environment.UserInteractive的判断上,
请看MSDN上面的解释:
获取一个值,用以指示当前进程是否在用户交互模式中运行。
UserInteractive 属性为运行时无用户界面的 Windows 进程或一个服务(如 IIS)报告 false。 如果此属性为 false,请不要显示模式对话框或消息框,因为没有用来与用户进行交互的图形用户界面。
http://msdn.microsoft.com/zh-cn/library/system.environment.userinteractive(v=VS.100).aspx
然后看一下Service1.cs中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text; namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
this.ServiceStart();
} protected override void OnStop()
{
this.ServiceStop();
} internal void DebugStart()
{
this.ServiceStart();
} internal void DebugStop()
{
this.ServiceStop();
} private void ServiceStart()
{
// TODO: } private void ServiceStop()
{
// TODO:
} }
}
最后:更改Project的输出类型
右击该Project,点击Properties,在Application标签中,更改Output Type为Console Application。
OK啦,按F5试试
调试windows服务最简单的方法之一的更多相关文章
- c#创建、安装、卸载、调试windows服务的简单事例
最近工作中用到了windows服务,对其有深刻理解和丰富经验谈不上,本篇文章只是简单陈诉用c#创建.安装.卸载.调试windows服务的步骤. 一.创建windows服务 1.用VS创建windows ...
- C# Asp.net Quartz.NET作业调度之创建、安装、卸载、调试windows服务的简单事例
一.创建windows服务 1.用VS创建windows服务,结果如下: 2.删除默认生成的Service1.cs文件,然后创建自己的服务文件(如:MyService),并修改Program.cs文件 ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- 使用C#调试Windows服务模板项目
Windows服务是非常强大的应用程序,可用于在backgorund中执行许多不同类型的任务.他们可以在不需要任何用户登录的情况下启动,并且可以使用除登录用户之外的其他用户帐户运行.但是,如果通过遵循 ...
- Windows服务二:测试新建的服务、调试Windows服务
一.测试Windows服务 为了使Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点.像其他应用程序一样,Windows服务也是在Program.cs的Main ...
- 如何调试 Windows 服务
概要 本文分步介绍了如何使用 WinDbg 调试程序 (windbg.exe) 调试 Windows 服务. 要调试 Windows 服务,可以在服务启动后将 WinDbg 调试程序附加到托管该服务的 ...
- 获取Windows服务下当前路径的方法
获取Windows服务下当前路径的方法 获取当前运行程序路径 包含exe Assembly.GetExecutingAssembly().Location; D:\xxxxxx\bin\Debug\x ...
- .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)
.net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...
- C#开发Windows服务 附简单实例实现禁止QQ运行
本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...
随机推荐
- SQL SERVER 内存
http://www.cnblogs.com/CareySon/archive/2012/08/16/HowSQLServerManageMemory.html
- Android设置TextView行间距(非行高)
Android设置TextView行间距(非行高) Android系统中TextView默认显示中文时会比较紧凑,不是很美观. 为了让每行保持一定的行间距,可以设置属性android:lineSpac ...
- nodeJS一些事儿
node-webkit:开发桌面+WEB混合型应用的神器[大漠穷秋] 展望未来 其实这条路老早就有人在走 网上有很多人在争论,未来究竟是原生的应用会胜出,还是WEB APP会胜出,实际上这两者并不是你 ...
- mysql数据库连接状态,不要做修改数据库表结构的操作;数据库迁移操作;
在开发过程中,python的flask框架使用sqlalmysql连接mysql数据库. 在程序连接数据量过程中,不要修改数据表的结构.比如在连接状态中使用下面的软件修改数据表结构,这个软件立即就会卡 ...
- 实现Activity的滑动返回效果
介绍 在知乎client上看到了这样的效果.左滑Activity能够返回到上一界面.非常适合单手操作. 找了非常久,最终在github上看到了SwipeBackLayout这个开源项目.地址: htt ...
- vue2.0 自定义 侧滑删除(LeftSlider)组件
1.自定义侧滑删除组件 LeftSlider.vue <!-- 侧滑删除 组件 --> <template> <div class="delete"& ...
- Odoo HRMS应用简介
Odoo HRMS包含行政管理的大部分功能,包含 部门组织架构 员工清册 岗位规划以及招聘管理 用工合同 考勤管理 休假和加班 费用报销 员工考核 绩效.激励.培训成绩 薪资清册 个角色 角色 ...
- valid-palindrome——判断带符号数字字母的字符串是否为回文
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 第一个Hello,OS World操作系统
来自:清泛网 - http://www.tsingfun.com/html/2015/dev_0804/hello_os_word_my_first_os.html 首先阐述下程序运行的基本原理:计算 ...
- LNMPA遇到504 Gateway time-out错误的解决方法
Nginx的特点是处理静态很给力,Apache的特点是处理动态很稳定,两者结合起来便是LNMPA,nginx处理前端,apache处理后端,这样处理静态会很快,处理动态会很稳定. 当我以为安装完成以后 ...