调试windows服务最简单的方法之一

http://item.jd.com/733388.html

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);
            }
        }
    }
}

  

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:
        }



    }
}

关键就在对Environment.UserInteractive的判断上,

请看MSDN上面的解释:

获取一个值,用以指示当前进程是否在用户交互模式中运行。

UserInteractive 属性为运行时无用户界面的 Windows 进程或一个服务(如 IIS)报告 false。 如果此属性为 false,请不要显示模式对话框或消息框,因为没有用来与用户进行交互的图形用户界面。

http://msdn.microsoft.com/zh-cn/library/system.environment.userinteractive(v=VS.100).aspx

然后看一下Service1.cs中的代码:

 

最后:更改Project的输出类型

右击该Project,点击Properties,在Application标签中,更改Output Type为Console Application。

OK啦,按F5试试

 

转载自http://www.cnblogs.com/builderman/archive/2011/06/14/2081045.html

转载于:https://www.cnblogs.com/jzzgci/p/4309340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值