Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 62
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

simple scheduler


Autor: no_me
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 5898
  
Kick it on dotnet-kicks.de  

Beschreibung:

simpler scheduler

Abgelegt unter: scheduler, timer, reseteventflag.



C#
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
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Threading;
using System.Timers;

namespace NetSnippetsQuickTest
{
    public interface ICommand
    {
        bool Execute();
        Guid Id { get; }
    }

    class Scheduler
    {
        List<ICommand> _commands;
        public readonly double SchedulingIntervalInMilliSeconds;
        public readonly int ExecutionRecurrences;
        int currentCommandIndex = 0;
        int currentRecurrence = 0;
        System.Timers.Timer _schedulingTimer;
        AutoResetEvent stopFlag;

        //CTOR with miliseconds use SchedulingIntervalInSeconds with x.0 or (double)
        public Scheduler(List<ICommand> Commands, double SchedulingIntervalInSeconds, int ExecutionRecurrences)
        {
            _commands = Commands;
            this.ExecutionRecurrences = ExecutionRecurrences;
            this.SchedulingIntervalInMilliSeconds = SchedulingIntervalInSeconds;
        }

        //CTOR with Minutes
        public Scheduler(List<ICommand> Commands, int SchedulingIntervalInMinutes, int ExecutionRecurrences)
            : this(Commands, ((double)SchedulingIntervalInMinutes * 60 * 1000), ExecutionRecurrences)
        {
        }

        public System.Collections.ObjectModel.ReadOnlyCollection<ICommand> Commands
        {
            get { return _commands.AsReadOnly(); }
        }

        public void Run()
        {
            //reset counter
            currentCommandIndex = 0;
            currentRecurrence = 0;
            stopFlag = new AutoResetEvent(false);
            _schedulingTimer = new System.Timers.Timer();
            //intervall is milliseconds 
            _schedulingTimer.Interval = SchedulingIntervalInMilliSeconds;
            _schedulingTimer.Elapsed += new ElapsedEventHandler(_schedulingTimer_Elapsed);
            _schedulingTimer.Enabled = true;
            //pause current thread here until scheduling is done
            stopFlag.WaitOne();
            //got signal from stopFlag so execution is finished
        }

        public bool IsFinished
        {
            get { return currentRecurrence == ExecutionRecurrences && currentCommandIndex == _commands.Count; }
        }

        void _schedulingTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //multiple timer calls spawn multiple threads so we have to sync this
            //so only one thread should execute this code
            lock (sender)
            {
                //maybe antoher thread has finished execution and we were blocked so we our job is done
                if (!IsFinished)
                {
                    //avoid out of range
                    ICommand cmd = (currentCommandIndex >= _commands.Count ? null : _commands[currentCommandIndex]);
                    if (cmd != null)
                    {
                        cmd.Execute();
                        currentCommandIndex++;
                    }
                    //we have reached the last command in queue so reset command index
                    if (currentCommandIndex == _commands.Count)
                    {
                        currentRecurrence++;
                        //we´re finished all necessary runs
                        if (IsFinished)
                        {
                            _schedulingTimer.Enabled = false;
                            stopFlag.Set();
                        }
                        else
                        {
                            currentCommandIndex = 0;
                        }
                    }
                }
            }
        }
    }
}

Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.