<?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>Ryan Vice&#039;s Blog &#187; WPF 3.5</title>
	<atom:link href="http://www.ryanvice.net/tag/wpf-3-5/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryanvice.net</link>
	<description>Implementation notes and development techniques</description>
	<lastBuildDate>Thu, 11 Feb 2010 13:43:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using BackgroundWorker with WPF</title>
		<link>http://www.ryanvice.net/wpf-3-5/using-backgroundworker-with-wpf/</link>
		<comments>http://www.ryanvice.net/wpf-3-5/using-backgroundworker-with-wpf/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 18:17:54 +0000</pubDate>
		<dc:creator>Ryan Vice</dc:creator>
				<category><![CDATA[WPF 3.5]]></category>
		<category><![CDATA[BackgroundWorker]]></category>
		<category><![CDATA[DispatcherSynchronizationContext]]></category>
		<category><![CDATA[SynchronizationContext]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.ryanvice.net/?p=186</guid>
		<description><![CDATA[Download Source
BackgroundWorker is a class that was introduced with in Windows Forms 2.0 to simplify executing work on background threads allowing UI developers to easily keep their UIs responsive by not tying up the UI thread. It&#8217;s a tool that was very useful and if you don&#8217;t already know then you&#8217;ll probably be very pleased [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ryanvice.net/wp-content/uploads/2009/12/WpfBackgroundWorker.zip">Download Source</a></p>
<p>BackgroundWorker is a class that was introduced with in Windows Forms 2.0 to simplify executing work on background threads allowing UI developers to easily keep their UIs responsive by not tying up the UI thread. It&#8217;s a tool that was very useful and if you don&#8217;t already know then you&#8217;ll probably be very pleased to find out that you can still use BackgroundWorker in WPF. This is because BackgroundWorker uses AsyncOperationManager which in turn uses SynchronizationContext to marshall work. In Windows Forms AsyncOperationManager gets a WindowsFormsSynchronizationContext class from the Forms application that derives from the SynchronizationContext class and uses it for marshalling. When using WPF a DispatcherSynchronizationContext is fetched by AsyncOperationManager which allows for easy marshalling of calls to background threads using BackgroundWorker.</p>
<p><span id="more-186"></span></p>
<p>Below is sample code for a simple form that will take your name as input and the write out &#8220;Hello &#8221; as output after a 5 second delay. If you run the application you will see that it is responsive during the delay and that you can resize and move the windows around as you should be able to when doing work on background threads. The demo also shows how to pass a value (your name) from the UI to the background thread.</p>
<h2>Xaml</h2>
<pre class="brush: xml;">&lt;Window x:Class=&quot;WpfBackgroundWorker.Window1&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    Title=&quot;Window1&quot; Height=&quot;300&quot; Width=&quot;300&quot;&gt;
    &lt;StackPanel&gt;
        &lt;TextBlock x:Name=&quot;Header&quot; &gt;Enter your name&lt;/TextBlock&gt;
        &lt;TextBox x:Name=&quot;InputTextBox&quot; /&gt;
        &lt;Button x:Name=&quot;DoWorkButton&quot; Content=&quot;Do work&quot; Click=&quot;DoWorkButton_Click&quot; /&gt;
        &lt;TextBlock x:Name=&quot;OutputTextBlock&quot; /&gt;
    &lt;/StackPanel&gt;
&lt;/Window&gt;
</pre>
<h2>Code behind</h2>
<pre class="brush: csharp;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace WpfBackgroundWorker
{
    /// &lt;summary&gt;
    /// Interaction logic for Window1.xaml
    /// &lt;/summary&gt;
    public partial class Window1 : Window
    {
        private BackgroundWorker _backgroundWorker;

        public Window1()
        {
            InitializeComponent();

            // Will use DispatcherSynchronizationContext
            _backgroundWorker = new BackgroundWorker();

            // Wire up event handlers
            _backgroundWorker.DoWork
                += new DoWorkEventHandler(_backgroundWorker_DoWork);
            _backgroundWorker.RunWorkerCompleted
                += new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);
        }

        void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            string result = (string)e.Result;

            if (e.Cancelled)
            {
                OutputTextBlock.Text = &quot;Cancelled&quot;;
            }
            else if (e.Error != null)
            {
                OutputTextBlock.Text = &quot;Exception: &quot; + e.Error.Message;
            }
            else
            {
                OutputTextBlock.Text = result;
            }
        }

        void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // Wait 5 seconds to simulate long running work
            Thread.Sleep(5000);

            // Assign the result to the Result property
            // of the DoWorkEventArgs
            // object. This is will be available to the
            // RunWorkerCompleted eventhandler.
            e.Result = &quot;Hello &quot; + e.Argument.ToString();
        }

        private void DoWorkButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(InputTextBox.Text))
            {
                MessageBox.Show(&quot;You must enter a name.&quot;);
                return;
            }

            // Do work on background thread
            _backgroundWorker.RunWorkerAsync(InputTextBox.Text);
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanvice.net/wpf-3-5/using-backgroundworker-with-wpf/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Hosting WorkflowRuntime 3.5 as a Windows Service</title>
		<link>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/</link>
		<comments>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 20:50:16 +0000</pubDate>
		<dc:creator>Ryan Vice</dc:creator>
				<category><![CDATA[WF 3.5]]></category>
		<category><![CDATA[Hosting]]></category>
		<category><![CDATA[SynchronizationContext]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[Windows Service]]></category>
		<category><![CDATA[WPF 3.5]]></category>

		<guid isPermaLink="false">http://www.ryanvice.net/?p=128</guid>
		<description><![CDATA[ Download Source
Hosting in a windows service has a few gotcha&#8217;s to it as I found out on my first WF project and I wanted to share those here to hopefully help others to get things running much quicker than I did.

Introduction
The code to host in a Windows service is the same as in any other [...]]]></description>
			<content:encoded><![CDATA[<p> <a href="http://www.ryanvice.net/wp-content/uploads/2009/11/WorkflowHostService.zip">Download Source</a></p>
<p>Hosting in a windows service has a few gotcha&#8217;s to it as I found out on my first WF project and I wanted to share those here to hopefully help others to get things running much quicker than I did.</p>
<p><span id="more-128"></span></p>
<h2>Introduction</h2>
<p>The code to host in a Windows service is the same as in any other windows application and you can find those details <a href="http://odetocode.com/Articles/457.aspx" target="_blank">here</a>. However, hosting in a Windows service needs some special handling and this is because you need to call <em>WorkflowRuntime.StartRuntime()</em>, <em>WorkflowRuntime.StopRuntime()</em>, and <em>WorkflowRuntime.Dispose()</em> on the same thread and the SCM (<a href="http://msdn.microsoft.com/en-us/library/ms685150(VS.85).aspx" target="_blank">Service Control Manager</a>) which is the Windows service that will call your Windows Services <em>OnStart()</em> and <em>OnStop</em>()<em> </em>methods will call these methods on different threads which is illustrated below. </p>
<p><a href="http://www.ryanvice.net/wp-content/uploads/2009/11/SCMThreading.JPG"><img class="alignnone size-full wp-image-134" title="SCMThreading" src="http://www.ryanvice.net/wp-content/uploads/2009/11/SCMThreading.JPG" alt="SCMThreading" width="837" height="469" /></a></p>
<p>You will want to put <em>WorkflowRuntime.StartRuntime() </em>in your <em>OnStart()</em> method and you will want to put your calls to <em>WorkflowRuntime.StopRuntime()</em> and <em>WorkflowRuntime.Dispose()</em> in the <em>OnStop()</em> method and you will need a way to make sure that all of these calls are preformed on the same thread or you will exprience issues like server hangs on shutdown.</p>
<h2>SyncronizationContext</h2>
<p>A custom SynchronizationContext is the tool that I used to preform calls on the same thread. My code is based on the threading <a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=313446" target="_blank">articles by mikeperetz</a> and I won&#8217;t be covering the threading details in this article so please see Mike&#8217;s articles for those details. I&#8217;ve taken the code in that article series and created my own custom SynchronizationContext called <em>MainThreadSynchronizationContext</em> which is included in the sample code and allows for you to easily marshal calls to the same thread by using the <em>Send</em> method shown below.</p>
<pre class="brush: csharp;">

    public partial class WorkflowServer : ServiceBase
    {
        private MainThreadSynchronizationContext _syncContext = null;
        WorkflowRuntime _runtime = null;

        public WorkflowServer()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // Create new thread using MainThreadSynchronizationContext and then
            // send message to call Start() on the new &quot;Main WF Thread&quot; thread
            _syncContext = new MainThreadSynchronizationContext(&quot;Main WF Thread&quot;);
            _syncContext.Send(Start, null);
        }

        // Will be called on &quot;Main WF Thread&quot;
        protected void Start(object data)
        {
            _runtime = new WorkflowRuntime();
            _runtime.StartRuntime();

            // Do other WF related processing
        }

        protected override void OnStop()
        {
            // Call Stop() on &quot;Main WF Thread&quot;
            _syncContext.Send(Stop, null);
        }

        // Will be called on &quot;Main WF Thread&quot;
        protected void Stop(object data)
        {
            _runtime.StopRuntime();
            _runtime.Dispose();
        }
    }</pre>
<p>If you use this technique your server will start and stop nicely and you won&#8217;t experience any hangs on shutdown which was something we were seeing a lot on my current project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ryanvice.net/wf3-5/hosting-workflowruntime-3-5-as-a-windows-service/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
