<?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</title>
	<atom:link href="http://www.ryanvice.net/tag/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ryanvice.net</link>
	<description>Implementation notes and development techniques</description>
	<lastBuildDate>Sun, 10 Oct 2010 12:34:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<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 [...]]]></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; title: ; notranslate">&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; title: ; notranslate">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>8</slash:comments>
		</item>
	</channel>
</rss>

