Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Molaro <lupus@oddwiz.org>2008-09-17 13:49:24 +0400
committerPaolo Molaro <lupus@oddwiz.org>2008-09-17 13:49:24 +0400
commited34912b86281f35d46949b4afb2929e6b7cca17 (patch)
tree7fe3713cf16e3810f12af50f562812deba1fc28d /mcs/class/System/System.Timers
parent64a00cfeadd4f9c0fd34b3a71f771e0190805596 (diff)
Wed Sep 17 11:48:00 CEST 2008 Paolo Molaro <lupus@ximian.com>
* Timer.cs: rewritten to use Threading.Timer so it doesn't create a thread per timer and behaves better. Fixed also a few implementation bugs. svn path=/trunk/mcs/; revision=113267
Diffstat (limited to 'mcs/class/System/System.Timers')
-rw-r--r--mcs/class/System/System.Timers/ChangeLog7
-rw-r--r--mcs/class/System/System.Timers/Timer.cs72
2 files changed, 24 insertions, 55 deletions
diff --git a/mcs/class/System/System.Timers/ChangeLog b/mcs/class/System/System.Timers/ChangeLog
index e7a242c2a05..e600215c163 100644
--- a/mcs/class/System/System.Timers/ChangeLog
+++ b/mcs/class/System/System.Timers/ChangeLog
@@ -1,3 +1,10 @@
+
+Wed Sep 17 11:48:00 CEST 2008 Paolo Molaro <lupus@ximian.com>
+
+ * Timer.cs: rewritten to use Threading.Timer so it doesn't create a
+ thread per timer and behaves better. Fixed also a few implementation
+ bugs.
+
2008-07-17 Jb Evain <jbevain@novell.com>
* Timer.cs: Fix ArgumentException message.
diff --git a/mcs/class/System/System.Timers/Timer.cs b/mcs/class/System/System.Timers/Timer.cs
index 48166c1688d..a0c024ff5ce 100644
--- a/mcs/class/System/System.Timers/Timer.cs
+++ b/mcs/class/System/System.Timers/Timer.cs
@@ -39,14 +39,10 @@ namespace System.Timers
[DefaultProperty("Interval")]
public class Timer : Component, ISupportInitialize
{
- bool autoReset;
- bool enabled;
- bool exiting;
double interval;
+ bool autoReset;
+ System.Threading.Timer timer;
ISynchronizeInvoke so;
- ManualResetEvent wait;
- WeakReference weak_thread;
- readonly object locker = new object ();
[Category("Behavior")]
[TimersDescription("Occurs when the Interval has elapsed.")]
@@ -83,25 +79,17 @@ namespace System.Timers
public bool Enabled
{
get {
- return enabled && !exiting;
+ return timer != null;
}
set {
if (Enabled == value)
return;
- enabled = value;
if (value) {
- if (exiting)
- StopTimer ();
- exiting = false;
- wait = new ManualResetEvent (false);
- Thread thread = new Thread (new ThreadStart (StartTimer));
- weak_thread = new WeakReference (thread);
-
- thread.IsBackground = true;
- thread.Start ();
+ timer = new System.Threading.Timer (Callback, this, (int)interval, autoReset? (int)interval: 0);
} else {
- StopTimer ();
+ timer.Dispose ();
+ timer = null;
}
}
}
@@ -119,6 +107,8 @@ namespace System.Timers
throw new ArgumentException ("Invalid value: " + value);
interval = value;
+ if (timer != null)
+ timer.Change ((int)interval, autoReset? (int)interval: 0);
}
}
@@ -174,51 +164,23 @@ namespace System.Timers
static void Callback (object state)
{
Timer timer = (Timer) state;
- if (timer.Elapsed == null)
+ ElapsedEventHandler events = timer.Elapsed;
+ if (!timer.autoReset)
+ timer.Enabled = false;
+ if (events == null)
return;
ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);
if (timer.so != null && timer.so.InvokeRequired) {
- timer.so.BeginInvoke (timer.Elapsed, new object [2] {timer, arg});
+ timer.so.BeginInvoke (events, new object [2] {timer, arg});
} else {
- timer.Elapsed (timer, arg);
- }
- }
-
- void StartTimer ()
- {
- WaitCallback wc = new WaitCallback (Callback);
-
- while (wait.WaitOne ((int) interval, false) == false) {
- exiting = !autoReset;
-
- ThreadPool.QueueUserWorkItem (wc, this);
-
- if (exiting)
- break;
- }
-
- lock (locker) {
- wait.Close ();
- wait = null;
+ try {
+ events (timer, arg);
+ } catch {
+ }
}
}
- void StopTimer ()
- {
- lock (locker) {
- if (wait != null)
- wait.Set ();
- }
-
- // the sleep speeds up the join under linux
- Thread.Sleep (0);
-
- Thread thread = (Thread)weak_thread.Target;
-
- if (thread != null)
- thread.Join ();
- }
}
}