2008-11-13 56 views
0

我有VS2005标准版和MS这样说:Windows服务,而不VS2005模板

注:Windows服务应用 项目模板和相关 功能不是在Visual Basic中的 标准版提供和 Visual C#.NET ...

是否可以在不升级VS2005标准版的情况下编写Windows服务应用程序?

回答

1

如果你可以剪切和粘贴,一个例子就够了。

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

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

谢谢!这就是我一直在寻找的! – 2008-11-17 12:08:16