* TextBoxBase.cs: Take HideSelection into account when
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / Timer.cs
blob3b26332245c1dcdfcb90aa7f750fdd6976663c12
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004 Novell, Inc.
22 // Authors:
23 // Jackson Harper (jackson@ximian.com)
26 using System;
27 using System.Threading;
28 using System.ComponentModel;
30 namespace System.Windows.Forms {
31 [DefaultProperty("Interval")]
32 [DefaultEvent("Tick")]
33 [ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Allow)]
34 public class Timer : Component {
36 private bool enabled;
37 private int interval = 100;
38 private DateTime expires;
39 internal Thread thread;
41 #if NET_2_0
42 object control_tag;
43 #endif
44 internal static readonly int Minimum = 15;
46 public Timer ()
48 enabled = false;
51 public Timer (IContainer container) : this ()
53 container.Add (this);
56 [DefaultValue (false)]
57 public virtual bool Enabled {
58 get {
59 return enabled;
61 set {
62 if (value != enabled) {
63 enabled = value;
64 if (value) {
65 // Use AddTicks so we get some rounding
66 expires = DateTime.UtcNow.AddMilliseconds (interval > Minimum ? interval : Minimum);
68 thread = Thread.CurrentThread;
69 XplatUI.SetTimer (this);
70 } else {
71 XplatUI.KillTimer (this);
72 thread = null;
78 [DefaultValue (100)]
79 public int Interval {
80 get {
81 return interval;
83 set {
84 if (interval == value) {
85 return;
88 interval = value;
90 // Use AddTicks so we get some rounding
91 expires = DateTime.UtcNow.AddMilliseconds (interval > Minimum ? interval : Minimum);
93 if (enabled == true) {
94 XplatUI.KillTimer (this);
95 XplatUI.SetTimer (this);
100 #if NET_2_0
101 [Localizable(false)]
102 [Bindable(true)]
103 [TypeConverter(typeof(StringConverter))]
104 [DefaultValue(null)]
105 [MWFCategory("Data")]
106 public object Tag {
107 get {
108 return control_tag;
111 set {
112 control_tag = value;
115 #endif
117 public void Start ()
119 Enabled = true;
122 public void Stop ()
124 Enabled = false;
127 internal DateTime Expires {
128 get {
129 return expires;
133 public event EventHandler Tick;
135 public override string ToString ()
137 return base.ToString () + ", Interval: " + Interval;
140 internal void Update (DateTime update)
142 expires = update.AddMilliseconds (interval > Minimum ? interval : Minimum);
145 internal void FireTick ()
147 OnTick (EventArgs.Empty);
151 protected virtual void OnTick (EventArgs e)
153 if (Tick != null)
154 Tick (this, e);
157 protected override void Dispose (bool disposing)
159 base.Dispose (disposing);
160 Enabled = false;
163 internal void TickHandler (object sender, EventArgs e)
165 OnTick (e);