<?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>Brant Burnett&#039;s Development Blog &#187; Threading</title>
	<atom:link href="http://btburnett.com/tag/threading/feed" rel="self" type="application/rss+xml" />
	<link>http://btburnett.com</link>
	<description></description>
	<lastBuildDate>Fri, 25 Mar 2011 13:39:35 +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>Event Handlers That Do Thread-Safe Invokes</title>
		<link>http://btburnett.com/2008/07/event-handlers-that-do-thread-safe-invokes.html</link>
		<comments>http://btburnett.com/2008/07/event-handlers-that-do-thread-safe-invokes.html#comments</comments>
		<pubDate>Fri, 11 Jul 2008 10:21:00 +0000</pubDate>
		<dc:creator>Brant Burnett</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://www.btburnett.com/?p=17</guid>
		<description><![CDATA[I recently ran into a scenario where I wanted to write a class to do a low-level serial interface to a barcode scanner. When the barcode is scanned I wanted it to fire an event back to the Windows form. The complication was that the SerialPort class provided by .NET 2.0 fires the DataReceived event [...]]]></description>
			<content:encoded><![CDATA[<div xmlns="http://www.w3.org/1999/xhtml"><small>I recently ran into a scenario where I wanted to write a class to do a low-level serial interface to a barcode scanner.  When the barcode is scanned I wanted it to fire an event back to the Windows form.  The complication was that the SerialPort class provided by .NET 2.0 fires the DataReceived event on an independent thread.  In order to fire the event back to the form it needs to be synchronized to run on the thread of the form using the Invoke method.</p>
<p>While this can be done using manual delegates and other methods, they don&#8217;t work very cleanly like an actual event handler does for simple development.  The fix to this problem is to actually write a custom event handler.  The custom event handler implements all of the back end handling that is normally hidden by VB.  Here is the example code from the barcode scanner class:<br /></small><br />
<blockquote><small>Private _barcodeScannedList As List(Of EventHandler(Of BarcodeScannedEventArgs))<br />Public Custom Event BarcodeScanned As EventHandler(Of BarcodeScannedEventArgs)
<div style="margin-left: 2em">AddHandler(ByVal value As EventHandler(Of BarcodeScannedEventArgs))
<div style="margin-left: 2em">If _barcodeScannedList Is Nothing Then
<div style="margin-left: 2em">_barcodeScannedList = New List(Of EventHandler(Of BarcodeScannedEventArgs))</div>
<p>End If<br />_barcodeScannedList.Add(value)</div>
<p>End AddHandler</p>
<p>RemoveHandler(ByVal value As EventHandler(Of BarcodeScannedEventArgs))
<div style="margin-left: 2em">If Not _barcodeScannedList Is Nothing Then
<div style="margin-left: 2em">_barcodeScannedList.Remove(value)</div>
<p>End If</div>
<p>End RemoveHandler</p>
<p>RaiseEvent(ByVal sender As Object, ByVal e As BarcodeScannedEventArgs)
<div style="margin-left: 2em">If Not _barcodeScannedList Is Nothing Then
<div style="margin-left: 2em">For Each handler As EventHandler(Of BarcodeScannedEventArgs) In _barcodeScannedList
<div style="margin-left: 2em">Dim safeInvoker As ISynchronizeInvoke = TryCast(handler.Target, ISynchronizeInvoke)<br />If safeInvoker Is Nothing Then
<div style="margin-left: 2em">handler.Invoke(sender, e)</div>
<p>Else
<div style="margin-left: 2em">safeInvoker.Invoke(handler, New Object() {sender, e})</div>
<p>End If</div>
<p>Next</div>
<p>End If</div>
<p>End RaiseEvent</div>
<p>End Event</small></p></blockquote>
<p><small>The _barcodeScannedList simply stores a list of all of the event handlers that have been registered with the class.  AddHandler and RemoveHandler do the process of actually adding and removing those event handlers from the list.  The real meat is in the RaiseEvent handler.  Instead of simply iterating through the list and firing all of the event delegates directly like the standard handler does, this handler tests to see if the target class of the delegate implements the ISynchronizeInvoke interface.  System.Windows.Forms.Control does implement this interface, meaning that all Forms implement it.  If this interface is found, then the interface&#8217;s Invoke method is used instead of the delegate&#8217;s Invoke method.  This will synchronize the call onto the delegate&#8217;s target&#8217;s thread.<br /></small></div>
]]></content:encoded>
			<wfw:commentRss>http://btburnett.com/2008/07/event-handlers-that-do-thread-safe-invokes.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

