1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2017 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.Windows.Forms;
23 using BSLib;
24 using GKCore.Interfaces;
25 
26 namespace GKUI.Platform
27 {
28     /// <summary>
29     /// WinForms-specific UI timer.
30     /// </summary>
31     public sealed class WinUITimer : BaseObject, ITimer
32     {
33         private readonly Timer fInnerTimer;
34         private readonly EventHandler fElapsedHandler;
35 
36         public bool Enabled
37         {
38             get { return fInnerTimer.Enabled; }
39             set {
40                 if (value) {
41                     fInnerTimer.Start();
42                 } else {
43                     fInnerTimer.Stop();
44                 }
45             }
46         }
47 
48         /// <summary>
49         /// Gets or sets the interval, in milliseconds.
50         /// </summary>
51         public double Interval
52         {
53             get { return fInnerTimer.Interval; }
54             set { fInnerTimer.Interval = (int)value; }
55         }
56 
WinUITimer(double msInterval, EventHandler elapsedHandler)57         public WinUITimer(double msInterval, EventHandler elapsedHandler)
58         {
59             fElapsedHandler = elapsedHandler;
60 
61             fInnerTimer = new Timer();
62             fInnerTimer.Interval = (int)msInterval;
63             fInnerTimer.Tick += ElapsedEventHandler;
64         }
65 
Dispose(bool disposing)66         protected override void Dispose(bool disposing)
67         {
68             if (disposing) {
69                 fInnerTimer.Dispose();
70             }
71             base.Dispose(disposing);
72         }
73 
ElapsedEventHandler(object sender, EventArgs e)74         private void ElapsedEventHandler(object sender, EventArgs e)
75         {
76             fElapsedHandler(sender, e);
77         }
78 
Start()79         public void Start()
80         {
81             fInnerTimer.Start();
82         }
83 
Stop()84         public void Stop()
85         {
86             fInnerTimer.Stop();
87         }
88     }
89 }
90