繁体   English   中英

没有VS2005模板的Windows服务

[英]Windows Service without the VS2005 template

我有VS2005标准版,MS表示:

注意:Windows Service Application项目模板和相关功能在Visual Basic和Visual C#.NET的标准版中不可用...

是否可以在不升级VS2005 Standard版本的情况下编写Windows Service应用程序?

如果可以剪切和粘贴,那么一个示例就足够了。

一个简单的服务,用于定期记录另一个服务的状态。 该示例不包括ServiceInstaller类 (在安装服务应用程序时由安装实用程序调用),因此安装是手动完成的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace SrvControl
{
    public partial class Service1 : ServiceBase
    {
        Timer mytimer;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (mytimer == null)
                mytimer = new Timer(5 * 1000.0);
            mytimer.Elapsed += new ElapsedEventHandler(mytimer_Elapsed);
            mytimer.Start();
        }

        void mytimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var srv = new ServiceController("MYSERVICE");
            AppLog.Log(string.Format("MYSERVICE Status {0}", srv.Status));
        }

        protected override void OnStop()
        {
            mytimer.Stop();
        }
    }
    public static class AppLog
    {
        public static string z = "SrvControl";
        static EventLog Logger = null;
        public static void Log(string message)
        {
            if (Logger == null)
            {
                if (!(EventLog.SourceExists(z)))
                    EventLog.CreateEventSource(z, "Application");

                Logger = new EventLog("Application");
                Logger.Source = z;
            }
            Logger.WriteEntry(message, EventLogEntryType.Information);
        }
    }
}

当然,您只需要自己编写代码。 实际上并不难。 以下是一些有关如何执行此操作的参考:

http://msdn.microsoft.com/zh-CN/magazine/cc301845.aspx

http://www.aspfree.com/c/a/C-Sharp/Creating-a-Windows-Service-with-C-Sharp-introduction/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM