先看一下这段启动代码:


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服务最简单的方法之一的更多相关文章

  1. c#创建、安装、卸载、调试windows服务的简单事例

    最近工作中用到了windows服务,对其有深刻理解和丰富经验谈不上,本篇文章只是简单陈诉用c#创建.安装.卸载.调试windows服务的步骤. 一.创建windows服务 1.用VS创建windows ...

  2. C# Asp.net Quartz.NET作业调度之创建、安装、卸载、调试windows服务的简单事例

    一.创建windows服务 1.用VS创建windows服务,结果如下: 2.删除默认生成的Service1.cs文件,然后创建自己的服务文件(如:MyService),并修改Program.cs文件 ...

  3. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  4. 使用C#调试Windows服务模板项目

    Windows服务是非常强大的应用程序,可用于在backgorund中执行许多不同类型的任务.他们可以在不需要任何用户登录的情况下启动,并且可以使用除登录用户之外的其他用户帐户运行.但是,如果通过遵循 ...

  5. Windows服务二:测试新建的服务、调试Windows服务

    一.测试Windows服务 为了使Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点.像其他应用程序一样,Windows服务也是在Program.cs的Main ...

  6. 如何调试 Windows 服务

    概要 本文分步介绍了如何使用 WinDbg 调试程序 (windbg.exe) 调试 Windows 服务. 要调试 Windows 服务,可以在服务启动后将 WinDbg 调试程序附加到托管该服务的 ...

  7. 获取Windows服务下当前路径的方法

    获取Windows服务下当前路径的方法 获取当前运行程序路径 包含exe Assembly.GetExecutingAssembly().Location; D:\xxxxxx\bin\Debug\x ...

  8. .net Windows服务程序和安装程序制作图解 及 VS 2010创建、安装、调试 windows服务(windows service)

    .net Windows服务程序和安装程序制作 最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作 ...

  9. C#开发Windows服务 附简单实例实现禁止QQ运行

    本实例主要实现下面三个基本功能 1.C#开发windows服务 2.禁止QQ等程序运行 3.为windows服务创建自动安装程序 下面针对这三个基本功能进行实现 一.C#开发windows服务 Win ...

随机推荐

  1. pt-pmp :pt toolkit

    http://www.cnblogs.com/ivictor/p/6012183.html

  2. eclipse主题下载网站

    http://eclipsecolorthemes.org/

  3. 一道简单DP题

    问题: 给定一个整数的数组,相邻的数不能同时选,求从该数组选取若干整数,使得他们的和最大,要求只能使用o(1)的空间复杂度.要求给出伪码. 解答: int maxSum(vector<int&g ...

  4. 开源软件许可认证:open softwae license

    OSIA认证的开放源代码软件的软件许可证有如下21种: 1.The GNU General Public License (GPL) 2.The GNU Library or "Lesser ...

  5. 借助树莓派模拟Wimonitor并实现WiFi窃听和嗅探

    Wimonitor是一款非常优秀的黑客工具,它不仅可以帮渗透测试人员省去配置虚拟机和无线网卡等一系列麻烦事,而且它的Web接口配置起来也非常的方便.实际上,它就是一款TP-Link-MR3020路由器 ...

  6. iOS开发 ----- 加载动画之牛顿摆的实现

    牛顿摆动画 自己看动画有一段时间了,但是还是不是很能理解其中的一些属性方法之类的东西,琢磨了一下午写了一个牛顿摆的动画,这里记录一下,一遍以后查看先上图 先说下思路 说下牛顿摆的大致运动过程 根据牛顿 ...

  7. angular - 新建项目 - 2

    ng new testNg 新建项目后,从网络上拉取模板(最后缓存下来,我们下次创建项目的时间将会减少80%) 安装过程中,需要我们提供Git账号和姓名 最后,我们进入 useNg 然后,启动服务器 ...

  8. java 发送微信客服消息

    package com.baosight.wechat.service; import net.sf.json.JSONObject; import org.apache.commons.httpcl ...

  9. 7.JAVA编程思想笔记隐藏实施过程

    欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/51040237 "进行面向对象的设计时,一项主要的考虑是:怎样将发生变 ...

  10. 怎样创建.NET Web Service http://blog.csdn.net/xiaoxiaohai123/article/details/1546941

    为什么需要Web Service 在通过internet网购买商品后,你可能对配送方式感到迷惑不解.经常的情况是因配送问题找配送公司而消耗你的大量时间,对于配送公司而言这也不是一项增值服务. 为了解决 ...