<?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>Dot Net Thoughts &#187; Background Worker</title>
	<atom:link href="http://www.dotnetthoughts.net/tag/background-worker/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetthoughts.net</link>
	<description>thoughts about .Net, WPF, Sharepoint, Javascript and more.</description>
	<lastBuildDate>Wed, 28 Jul 2010 09:59:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using background worker in C#</title>
		<link>http://www.dotnetthoughts.net/2009/08/19/using-background-worker-in-c/</link>
		<comments>http://www.dotnetthoughts.net/2009/08/19/using-background-worker-in-c/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 07:57:47 +0000</pubDate>
		<dc:creator>Anuraj P</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[.Net 3.0 / 3.5]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[Background Worker]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Threading]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://anuraj.wordpress.com/?p=366</guid>
		<description><![CDATA[Background worker is a component introduced by Microsoft in .Net 2.0 which will help developers to do background operations without the knowledge of threading and deadlocks. In the current application I am working we used to copy some files from hard drive to specified USB drive, based on some criteria. For the IO operation we [...]]]></description>
			<content:encoded><![CDATA[<p>Background worker is a component introduced by Microsoft in .Net 2.0 which will help developers to do background operations without the knowledge of threading and deadlocks. In the current application I am working we used to copy some files from hard drive to specified USB drive, based on some criteria. For the IO operation we used some background worker, but for the progress reporting we used a very easy method, we just put a progress bar and set the Style property to Marquee. It will display a block moving always from right to left. We were aware of the ProgressChanged event, but when I tried it, it throws some cross thread exception. After working around the documentation in MSDN I found the solution for this.</p>
<p>Example Code</p>
<p>Make sure the background worker WorkerReportProgress Property set to</p>
<pre class="brush: csharp;">

//BackGround Worker DoWork Event.
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
//Do the long running job here
int index = 0;
while(true)
{
	if(index &gt;= 100)
	{
		break;
	}
	index ++;
	Thread.Sleep(100);
	//The second parameter is required only
	//if you want to display some output
	//Here we are updating the Progress.
	this.bgWorker.ReportProgress(index, index);
}
}

//BackGround Worker Progress Changed Event.
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
	this.pbStatus.Value = e.ProgressPercentage;
	this.lstValues.Items.Add(e.UserState);
}

//BackGround Worker Work completed Event.
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
	MessageBox.Show(&quot;Successfully completed&quot;);
}
</pre>
<p>In this code, I am counting from 0 to 100, also updating a Progress bar and Listbox in Userinterface with the values.</p>
<p>Links :<br />
<a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx" target="_blank">Back Ground worker in MSDN</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetthoughts.net/2009/08/19/using-background-worker-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
