Autor Beitrag
elur
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 08.05.09 13:48 
Hallo Gemeinde,

ich versuch mich grad an einem eigenen Windows Dienst. Folgenden Code hab ich:

ausblenden volle Höhe C#-Quelltext
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
    {

        // This is a flag to indicate the service status
        private bool serviceStarted = false;

        // the thread that will do the work
        Thread workerThread;


        public Service1()
        {
            InitializeComponent();

            // Ereignissanzeige Log besorgen
            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");

            // Create worker thread, this worker thread will invoke the WorkerFunction when we start it
            // reason: when we use a separate worker thread, the main service
            // thread will return quickly, telling Windows that service has started
            ThreadStart st = new ThreadStart(WorkerFunction);
            workerThread = new Thread(st);

            // start the  worker thread
            workerThread.Start();

            // set flag to indicate worker thread is active
            serviceStarted = true;

        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("inOnStop");

            // flag to tell the worker thread to stop
            serviceStarted = false;

        }

        protected override void OnPause()
        {
            eventLog1.WriteEntry("inOnPause");
        }

        protected override void OnContinue()
        {
            eventLog1.WriteEntry("inOnContinue");
        }


        /// <summary>
        /// This function will do all the work
        /// Once it is done with its tasks, it will be suspended for some time;
        /// it will continue to repeat this until the service is stopped
        /// </summary>
        private void WorkerFunction()
        {
           // start an endless loop; loop will end only when "serviceStarted" flag = false
           while (serviceStarted)
           {
              // do something
              // exception handling omitted here for simplicity
              EventLog.WriteEntry("Service working", System.Diagnostics.EventLogEntryType.Information);
              eventLog1.WriteEntry("Service is working");
         
              // yield
              if (serviceStarted)
              {
                 Thread.Sleep(new TimeSpan(020));
              }
           }
         
           // time to end the thread
           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
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 08.05.09 17:50 
:welcome:

Es ist zwar unwahrscheinlich (wie alle Race-Conditions ;) ), aber vielleicht ist serviceStarted bei Threadbeginn einfach noch auf false? Setze es doch mal direkt in WorkerFunction auf true.
Und kurz vor dem natürlichen Ende des Threads hat Abort auch nicht mehr viel Wirkung ;) .

_________________
>λ=
elur Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 08.05.09 20:33 
danke für den willkommensgruß :wink2:

daran lags leider nicht, hatte ein kleine fehler hier:
ausblenden C#-Quelltext
1:
2:
3:
4:
// start the  worker thread            
workerThread.Start();            
// set flag to indicate worker thread is active            
serviceStarted = true;


... macht natürlich wenig sinn erst den thread zu starten und dann den flag auf true zu setzen.
aber egal wie rum auch wenn ich die WorkerFunction() mit serviceStarted = true; beginne passiert einfach nichts.

danke und gruß
elur
elur Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Fr 08.05.09 21:29 
is mir ja peinlich aber der fehler saß ma wieder vor dem bildschirm. das setup projekt muss ja jedesmal mit neu erstellt werden ... warum is mir zwar (noch) nicht ganz klar aber dann funktioniert alles bestens.

danke und gruß
elur