Hallo Gemeinde,
ich versuch mich grad an einem eigenen Windows Dienst. Folgenden Code hab ich:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87:
| public partial class Service1 : ServiceBase {
private bool serviceStarted = false;
Thread workerThread;
public Service1() { InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyNewLog"; }
protected override void OnStart(string[] args) { eventLog1.WriteEntry("inOnStart");
ThreadStart st = new ThreadStart(WorkerFunction); workerThread = new Thread(st);
workerThread.Start();
serviceStarted = true;
}
protected override void OnStop() { eventLog1.WriteEntry("inOnStop");
serviceStarted = false;
}
protected override void OnPause() { eventLog1.WriteEntry("inOnPause"); }
protected override void OnContinue() { eventLog1.WriteEntry("inOnContinue"); }
private void WorkerFunction() { while (serviceStarted) { EventLog.WriteEntry("Service working", System.Diagnostics.EventLogEntryType.Information); eventLog1.WriteEntry("Service is working"); if (serviceStarted) { Thread.Sleep(new TimeSpan(0, 2, 0)); } } Thread.CurrentThread.Abort(); } } |
nun hab ich folgendes problem: Ich kann den Dienst fehlerfrei installieren und deinstallieren, auch starten, pause, fortsetzen und stoppen funktioniert. Er trägt auch fleißig die entsprechenden Einträge ins log ein ABER nichts aus der WorkerFunction() ... d.h. ich weiß garnicht wirklich ob er den thread wirklich startet ...
hat jemand eine idee was an dem code nicht stimmt??
vielen dank im vorraus
elur