From 9f19cd09bfad13715bb4eda46e7782f56674e26c Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Fri, 29 Mar 2013 14:58:12 +0400
Subject: using PersistentImageCache for avatars
---
Juick/Threading/OneShotDispatcherTimer.cs | 160 ++++++++++++++++++++++++++++++
1 file changed, 160 insertions(+)
create mode 100644 Juick/Threading/OneShotDispatcherTimer.cs
(limited to 'Juick/Threading')
diff --git a/Juick/Threading/OneShotDispatcherTimer.cs b/Juick/Threading/OneShotDispatcherTimer.cs
new file mode 100644
index 0000000..4b2854d
--- /dev/null
+++ b/Juick/Threading/OneShotDispatcherTimer.cs
@@ -0,0 +1,160 @@
+// Copyright 2010 Andreas Saudemont (andreas.saudemont@gmail.com)
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Windows.Threading;
+
+namespace Kawagoe.Threading
+{
+ ///
+ /// Provides a one-shot timer integrated to the Dispatcher queue.
+ ///
+ public class OneShotDispatcherTimer
+ {
+ ///
+ /// Creates a new and starts it.
+ ///
+ /// The duration of the timer.
+ /// The delegate that will be called when the timer fires.
+ /// The newly created timer.
+ public static OneShotDispatcherTimer CreateAndStart(TimeSpan duration, EventHandler callback)
+ {
+ OneShotDispatcherTimer timer = new OneShotDispatcherTimer();
+ timer.Duration = duration;
+ timer.Fired += callback;
+ timer.Start();
+ return timer;
+ }
+
+ private TimeSpan _duration = TimeSpan.Zero;
+ private DispatcherTimer _timer = null;
+
+ ///
+ /// Initializes a new instance.
+ ///
+ public OneShotDispatcherTimer()
+ {
+ }
+
+ ///
+ /// The duration of the timer. The default is 00:00:00.
+ ///
+ ///
+ /// Setting the value of this property takes effect the next time the timer is started.
+ ///
+ /// The specified value when setting this property represents
+ /// a negative time internal.
+ public TimeSpan Duration
+ {
+ get
+ {
+ return _duration;
+ }
+ set
+ {
+ if (value.TotalMilliseconds < 0)
+ {
+ throw new ArgumentOutOfRangeException();
+ }
+ _duration = value;
+ }
+ }
+
+ ///
+ /// Indicates whether the timer is currently started.
+ ///
+ public bool IsStarted
+ {
+ get
+ {
+ return (_timer != null);
+ }
+ }
+
+ ///
+ /// Occurs when the one-shot timer fires.
+ ///
+ public event EventHandler Fired;
+
+ ///
+ /// Raises the event.
+ ///
+ private void RaiseFired()
+ {
+ if (Fired != null)
+ {
+ try
+ {
+ Fired(this, EventArgs.Empty);
+ }
+ catch (Exception) { }
+ }
+ }
+
+ ///
+ /// Starts the timer.
+ /// This method has no effect if the timer is already started.
+ ///
+ ///
+ /// The same instance can be started and stopped multiple times.
+ ///
+ public void Start()
+ {
+ if (_timer != null)
+ {
+ return;
+ }
+
+ _timer = new DispatcherTimer();
+ _timer.Interval = _duration;
+ _timer.Tick += OnTimerTick;
+ _timer.Start();
+ }
+
+ ///
+ /// Stops the timer.
+ /// This method has no effect if the timer is not started.
+ ///
+ ///
+ /// The event is guaranteed not to be raised once this method has been invoked
+ /// and until the timer is started again.
+ ///
+ public void Stop()
+ {
+ if (_timer == null)
+ {
+ return;
+ }
+ try
+ {
+ _timer.Stop();
+ }
+ catch (Exception) { }
+ _timer = null;
+ }
+
+ ///
+ /// Listens to Tick events on the underlying timer.
+ ///
+ private void OnTimerTick(object sender, EventArgs e)
+ {
+ if (sender != _timer)
+ {
+ return;
+ }
+ Stop();
+ RaiseFired();
+ }
+ }
+}
--
cgit v1.2.3