<?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>dotnet 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>a dotnet developer&#039;s technical blog</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:18:00 +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>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 &#8230; <a href="http://www.dotnetthoughts.net/2009/08/19/using-background-worker-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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; title: ; notranslate">

//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>
<div class="betterrelated"><p><strong>Related content:</strong></p>
<ol><li> <a href="http://www.dotnetthoughts.net/2008/06/16/working-with-backgroundworker-class/" title="Permanent link to Working with backgroundworker class">Working with backgroundworker class</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2010/02/05/system_invalidoperationexception_cross_thread_operation_not_valid/" title="Permanent link to System.InvalidOperationException – Cross-thread operation not valid">System.InvalidOperationException – Cross-thread operation not valid</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/10/consuming-a-c-dll-in-c/" title="Permanent link to Consuming a C++ DLL in C#">Consuming a C++ DLL in C#</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2009/07/04/drag-and-drop-files-from-windows-to-your-application/" title="Permanent link to Drag and Drop files from Windows to your application">Drag and Drop files from Windows to your application</a>  </li>
<li> <a href="http://www.dotnetthoughts.net/2008/06/12/calling-synchronous-methods-asynchronously-ii/" title="Permanent link to Calling Synchronous Methods Asynchronously &#8211; II">Calling Synchronous Methods Asynchronously &#8211; II</a>  </li>
</ol></div>]]></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>

