Skip to content

Commit

Permalink
2009-09-27 Sebastien Pouliot <sebastien@ximian.com>
Browse files Browse the repository at this point in the history
	* Timer_2_1.cs: New. A smaller, internal subset of Timer for 
	Moonlight - needed for Socket.Close(int)
	[Backport of r142718]


svn path=/branches/mono-2-6/mcs/; revision=142719
  • Loading branch information
Sebastien Pouliot committed Sep 27, 2009
1 parent d1973e1 commit cb768e7
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mcs/class/System/System.Timers/ChangeLog
@@ -1,3 +1,9 @@
2009-09-27 Sebastien Pouliot <sebastien@ximian.com>

* Timer_2_1.cs: New. A smaller, internal subset of Timer for
Moonlight - needed for Socket.Close(int)
[Backport of r142718]

2009-09-24 Gonzalo Paniagua Javier <gonzalo@novell.com>

* Timer.cs: lock access to the 'timer' field. Attemp to fix bug
Expand Down
114 changes: 114 additions & 0 deletions mcs/class/System/System.Timers/Timer_2_1.cs
@@ -0,0 +1,114 @@
//
// System.Timers.Timer - Moonlight specific version for sockets
//
// Authors:
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002 Ximian, Inc (http://www.ximian.com)
// Copyright (C) 2005,2009 Novell, Inc (http://www.novell.com)
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System.ComponentModel;
using System.Threading;

namespace System.Timers {

internal class Timer {
double interval;
bool autoReset;
System.Threading.Timer timer;
object _lock = new object ();

public event ElapsedEventHandler Elapsed;

public Timer ()
{
autoReset = true;
Interval = 100;
}

public bool AutoReset
{
get { return autoReset; }
set { autoReset = value; }
}

public bool Enabled
{
get {
lock (_lock)
return timer != null;
}
set {
lock (_lock) {
bool enabled = timer != null;
if (enabled == value)
return;

if (value) {
timer = new System.Threading.Timer (Callback, this, (int)interval, autoReset ? (int)interval: 0);
} else {
timer.Dispose ();
timer = null;
}
}
}
}

public double Interval
{
get { return interval; }
set {
// The doc says 'less than 0', but 0 also throws the exception
if (value <= 0)
throw new ArgumentException ("Invalid value: " + value);

lock (_lock) {
interval = value;
if (timer != null)
timer.Change ((int)interval, autoReset? (int)interval: 0);
}
}
}

static void Callback (object state)
{
Timer timer = (Timer) state;
if (timer.Enabled == false)
return;
ElapsedEventHandler events = timer.Elapsed;
if (!timer.autoReset)
timer.Enabled = false;
if (events == null)
return;

ElapsedEventArgs arg = new ElapsedEventArgs (DateTime.Now);

try {
events (timer, arg);
} catch {
}
}
}
}

0 comments on commit cb768e7

Please sign in to comment.