<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Progressive Digressive &#187; Bugs</title>
	<atom:link href="http://www.marcuswhitworth.com/category/bugs/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.marcuswhitworth.com</link>
	<description>Marcus Whitworth&#039;s tech blog – .NET, C#, Silverlight, WPF, Flex…etc, etc</description>
	<lastBuildDate>Wed, 14 Dec 2011 16:14:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rx &#8211; System Time and Scheduler issues</title>
		<link>http://www.marcuswhitworth.com/2011/09/rx-system-time-and-scheduler-issues/</link>
		<comments>http://www.marcuswhitworth.com/2011/09/rx-system-time-and-scheduler-issues/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 16:36:06 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[rx]]></category>

		<guid isPermaLink="false">http://www.marcuswhitworth.com/?p=188</guid>
		<description><![CDATA[I recently had an issue with time-dependent methods of Rx, which were adversely affected when a user changed their system clock.  You can read up on the history on the Rx forums. In the older Rx v1.0.2787 (which my current &#8230; <a href="http://www.marcuswhitworth.com/2011/09/rx-system-time-and-scheduler-issues/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I recently had an issue with time-dependent methods of Rx, which were adversely affected when a user changed their system clock.  You can read up on the history <a href="http://social.msdn.microsoft.com/Forums/en/rx/thread/672a9b1d-c25e-4797-ad24-5f3252089df0" target="_blank">on the Rx forums</a>.</p>
<p>In the older Rx v1.0.2787 (which my current project is still using), <em>BufferWithTime</em> was affected, along with other methods such as <em>Interval, Timer, </em>and<em> Sample. </em></p>
<p>The most problematic for my purposes was <em>BufferWithTime</em><em>. </em> For instance, if you shift the system clock back and then push values onto the Buffer, the values are then either lost completely or they arrive late.  If you shift the clock forward a significant amount, Rx overloads the CPU pushing through potentially thousands of empty updates in an effort to catch up with the new clock time.  Worse still, if you shift your clock back several years then you get an UnhandledException along the lines of &#8220;Time-out interval must be less than 2^32-2&#8243;.</p>
<p>In the latest Rx v1.1.10621 (experimental), it seems neither <em>Buffer </em>nor <em>Window</em> are affected, but <em>Interval, Timer, </em>and<em> Sample</em> are.</p>
<p>In short, the issue is that all problematic methods are dependent upon <em>IScheduler.Now</em>.  In all <em>IScheduler</em> implementations, this property just returns <em>DateTimeOffset.Now</em>, which is of course straight from the system clock.  Change the system clock, and subsequent reads of <em>IScheduler.Now</em> will return times not in line with previous values.</p>
<p>The solution was to implement a custom <em>IScheduler</em>.  I&#8217;ve quite literally copied the <em>ThreadPoolScheduler</em> source, and simply modified the <em>Now</em> property implementation.</p>
<p>In my implementation below, you can see that I start a <em>Stopwatch</em> during construction, and use this to determine the actual time in the getter for <em>Now</em>.  From my testing it behaves correctly in all situations where I was previously missing notifications.  The relevant parts are the constructor and the <em>Now</em> getter, all other methods are simply a copy of the <em>ThreadPoolScheduler</em>.</p>
<p><a href="http://www.marcuswhitworth.com/wp-content/uploads/2011/09/RxTimerTests.zip">Attached here</a> is a sample project demonstrating both the issue and the fix for the latest Rx release.  Just run it and hit the &#8220;-10 secs&#8221; button to shift your system clock back.</p>
<p>Thoughts and comments welcome&#8230;</p>
<pre class="brush:csharp">public class AccurateTimeScheduler : IScheduler, IDisposable
{
	private static readonly object _gate = new object();
	private static readonly Dictionary&lt;Timer, object&gt; _timers 
		= new Dictionary&lt;Timer, object&gt;();

	private readonly Stopwatch _stopWatch = new Stopwatch();
	private DateTimeOffset _startTime;

	public AccurateTimeScheduler()
	{
		_startTime = DateTimeOffset.Now;
		_stopWatch.Start();
	}

	public DateTimeOffset Now
	{
		get
		{
			var dateTimeOffset = _startTime.AddMilliseconds(
				_stopWatch.ElapsedMilliseconds);

			Trace.WriteLine(
				string.Format("Now: Default={0}, Corrected={1}",
					            DateTimeOffset.Now.ToString("HH:mm:ss.fff"),
					            dateTimeOffset.ToString("HH:mm:ss.fff")));
			return _startTime.AddMilliseconds(_stopWatch.ElapsedMilliseconds);
		}
	}

	public IDisposable Schedule&lt;TState&gt;(TState state, Func&lt;IScheduler, TState, IDisposable&gt; action)
	{
		if (action == null)
			throw new ArgumentNullException("action");
		var d = new SingleAssignmentDisposable();
		ThreadPool.QueueUserWorkItem(
			_ =&gt;
				{
					if (d.IsDisposed)
						return;
					d.Disposable = action((IScheduler) this, state);
				}, null);
		return d;
	}

	public IDisposable Schedule&lt;TState&gt;(TState state, TimeSpan dueTime, Func&lt;IScheduler, TState, IDisposable&gt; action)
	{
		if (action == null)
			throw new ArgumentNullException("action");
		var dueTime1 = Scheduler.Normalize(dueTime);
		if (dueTime1.Ticks == 0L)
			return Schedule(state, action);
		var hasAdded = false;
		var hasRemoved = false;
		var d = new MultipleAssignmentDisposable();
		Timer timer = null;
		timer = new Timer(
			_ =&gt;
				{
					Func&lt;IScheduler, TState, IDisposable&gt; localAction;
					lock (_gate)
					{
						if (hasAdded &amp;&amp; timer != null)
							_timers.Remove(timer);
						localAction = action;
					}
					var localTimer = timer;
					if (localTimer != null)
						localTimer.Dispose();
					timer = null;
					if (localAction != null)
					{
						d.Disposable = localAction((IScheduler) this, state);
						Trace.WriteLine("Executing");
					}
					action = null;
				}, null, dueTime1, TimeSpan.FromMilliseconds(-1.0));

		lock (_gate)
		{
			if (!hasRemoved)
			{
				_timers.Add(timer, null);
				hasAdded = true;
			}
		}

		d.Disposable = Disposable.Create(
			() =&gt;
				{
					var localTimer = timer;
					if (localTimer != null)
					{
						localTimer.Dispose();
						lock (_gate)
						{
							_timers.Remove(localTimer);
							action = null;
						}
					}
					timer = null;
				});
		return d;
	}

	public IDisposable Schedule&lt;TState&gt;(
TState state, DateTimeOffset dueTime, Func&lt;IScheduler, TState, IDisposable&gt; action)
	{
		if (action == null)
			throw new ArgumentNullException("action");

		return Schedule(state, dueTime - Now, action);
	}

	public void Dispose()
	{
		_stopWatch.Stop();
	}
}</pre>
<div class="shr-publisher-188"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.marcuswhitworth.com%2F2011%2F09%2Frx-system-time-and-scheduler-issues%2F' data-shr_title='Rx+-+System+Time+and+Scheduler+issues'></a><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.marcuswhitworth.com%2F2011%2F09%2Frx-system-time-and-scheduler-issues%2F' data-shr_title='Rx+-+System+Time+and+Scheduler+issues'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.marcuswhitworth.com/2011/09/rx-system-time-and-scheduler-issues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio Silverlight/xaml bug</title>
		<link>http://www.marcuswhitworth.com/2009/09/visual-studio-silverlight-xaml-bug/</link>
		<comments>http://www.marcuswhitworth.com/2009/09/visual-studio-silverlight-xaml-bug/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 17:45:42 +0000</pubDate>
		<dc:creator>Marcus</dc:creator>
				<category><![CDATA[Bugs]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[silverlight]]></category>
		<category><![CDATA[visual studio]]></category>

		<guid isPermaLink="false">http://www.marcuswhitworth.com/?p=60</guid>
		<description><![CDATA[This one was driving me crazy for at least a few hours.  On a fresh install of VS2008 &#38; Silverlight 3 tools, there was no xaml code highlighting at all, no intellisense, nothing &#8211; just like any ordinary text file.  &#8230; <a href="http://www.marcuswhitworth.com/2009/09/visual-studio-silverlight-xaml-bug/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This one was driving me crazy for at least a few hours.  On a fresh install of VS2008 &amp; Silverlight 3 tools, there was no xaml code highlighting at all, no intellisense, nothing &#8211; just like any ordinary text file.  Checking the same project on another machine, it was all fine.  So I starting disabling/uninstalling all VS plugins (ReSharper, AnkhSVN), but still no luck.  Was starting to think I&#8217;d have to reinstall VS&#8230;</p>
<p><a href="http://www.tipsdotnet.com/TechBlog.aspx?PageIndex=0&amp;BLID=12" target="_blank">The solution</a> was simple enough &#8211; run the VS Command Prompt, and enter:</p>
<pre>devenv /resetskippkgs</pre>
<p>Problem solved.  <a href="http://msdn.microsoft.com/en-us/library/ms241276%28VS.80%29.aspx" target="_blank">Apparently a good one to try</a> whenever you lose formatting or Intellisense features.</p>
<div class="shr-publisher-60"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F09%2Fvisual-studio-silverlight-xaml-bug%2F' data-shr_title='Visual+Studio+Silverlight%2Fxaml+bug'></a><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.marcuswhitworth.com%2F2009%2F09%2Fvisual-studio-silverlight-xaml-bug%2F' data-shr_title='Visual+Studio+Silverlight%2Fxaml+bug'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.marcuswhitworth.com/2009/09/visual-studio-silverlight-xaml-bug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 4/11 queries in 0.003 seconds using disk: basic
Object Caching 616/629 objects using disk: basic

Served from: www.marcuswhitworth.com @ 2012-02-06 22:06:59 -->
