2008-11-13 6 views
0

J'ai l'édition standard VS2005 et MS dit ceci:service Windows sans le modèle VS2005

Remarque: Les modèles de projet de service d'application Windows et fonctionnalité associées ne sont pas disponibles dans l'édition standard de Visual Basic et .NET Visual C# ...

Est-il possible d'écrire une application de service Windows sans mettre à jour mon VS2005 standard Edition?

Répondre

1

Si vous pouvez couper et coller, un exemple suffit.

Un service simple pour consigner périodiquement l'état d'un autre service. L'exemple n'inclut pas le ServiceInstaller class (appelé par l'utilitaire d'installation lors de l'installation d'une application de service), l'installation est donc manuelle.

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

Merci! C'était ce que je cherchais! –