<?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/"
	>

<channel>
	<title>Patrick Leblanc's Blog</title>
	<atom:link href="http://www.pleblanc.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pleblanc.com</link>
	<description></description>
	<pubDate>Mon, 25 Jan 2010 06:33:07 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Managing multiple threads in C# with a GUI</title>
		<link>http://www.pleblanc.com/2009/06/managing-multiple-threads-in-c-with-a-gui/</link>
		<comments>http://www.pleblanc.com/2009/06/managing-multiple-threads-in-c-with-a-gui/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 21:19:24 +0000</pubDate>
		<dc:creator>Patrick Leblanc</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[Delegates]]></category>

		<category><![CDATA[GUI]]></category>

		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://www.pleblanc.com/?p=39</guid>
		<description><![CDATA[Objective: manage multiple threads from C# with a responsive GUI
Description:: In this example, I simulate encoding from a source that is splittable in segments. It could be the b-frames between I-frames in a video or even some sort of audio codec. To simulate the workload, I use Thread.Sleep(TimeInMs).

For this code, I have 4 threads working [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Objective</strong>: manage multiple threads from C# with a responsive GUI<br />
<strong>Description:</strong>: In this example, I simulate encoding from a source that is splittable in segments. It could be the b-frames between I-frames in a video or even some sort of audio codec. To simulate the workload, I use Thread.Sleep(TimeInMs).</p>
<p><a href="http://www.pleblanc.com/wp-content/uploads/2009/06/bggui.png"><img src="http://www.pleblanc.com/wp-content/uploads/2009/06/bggui-150x150.png" alt="bggui" title="bggui" width="150" height="150" class="alignnone size-thumbnail wp-image-47" /></a></p>
<p>For this code, I have 4 threads working on a source to encode of 10 segments. Each threads will take a slice to encode, process it and reassemble the end result in an array, that can be utilized by another process to unify the encoded version, for example.</p>
<p>A word of caution though: although working with threads can be useful in some cases it also complicates the code quite a bit, especially at debugging. It&#8217;s much harder to keep track of what is going on.</p>
<p>Here is the code in the windows form (download the whole project in VS2008 at the end):</p>
<p><span id="more-39"></span></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
<span style="color: #0600FF;">namespace</span> bgGUI
<span style="color: #000000;">&#123;</span>
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Drawing</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> Form1 <span style="color: #008000;">:</span> Form
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> setTextBoxTextCallback<span style="color: #000000;">&#40;</span>ArrayList a<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> setLabelTextCallback<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> s, Label l<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//delegate used to set the text on a label in a Invoke operation</span>
        ArrayList videoSegments<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//Imagine this as a collection video/audio segments we need to compute.</span>
        <span style="color: #FF0000;">int</span> workingIndex<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// This is to track where we're at in the videoSegments during the process.</span>
        <span style="color: #FF0000;">object</span> lockObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// the lock object used for the thread to safely pick a new segment to compute.</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Form1_Load<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//Here we're gonna fake that the stack of videoSegements is full of 10 slice to encode.</span>
            videoSegments <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">20</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                videoSegments.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;this is segement &quot;</span> <span style="color: #008000;">+</span> i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">//for the purpose of the demonstration I'm adding a string and its index.</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            workingIndex <span style="color: #008000;">=</span> videoSegments.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
            label5.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> videoSegments.<span style="color: #0000FF;">Count</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//We're starting 4 different thread with parameters (hence the use of the ParameterizedThreadStart Delegate)</span>
            <span style="color: #008080; font-style: italic;">//Becareful though as ParameterizedThreadStart only takes method with an objet argument. Therefore it's not type safe.</span>
            <span style="color: #008080; font-style: italic;">//an alternative, type safe way of doing it is to create an helper class. and start it with ThreadStart</span>
            <span style="color: #008080; font-style: italic;">//more details here: http://msdn.microsoft.com/en-us/library/ts553s52.aspx</span>
            <span style="color: #008080; font-style: italic;">//In my example, I pass as arguments a Label and an int, a time interval in ms.</span>
            Thread t1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>tsChangeLabelText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            t1.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> label1, <span style="color: #FF0000;">2500</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Thread t2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>tsChangeLabelText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            t2.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> label2, <span style="color: #FF0000;">3500</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Thread t3 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>tsChangeLabelText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            t3.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> label3, <span style="color: #FF0000;">1000</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Thread t4 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thread<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ParameterizedThreadStart<span style="color: #000000;">&#40;</span>tsChangeLabelText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            t4.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> label4, <span style="color: #FF0000;">1250</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> tsChangeLabelText<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> o<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//we cast the object as an array.</span>
            <span style="color: #008080; font-style: italic;">//then we exatract the first object at index[x], then we cast it to what we want</span>
            <span style="color: #008080; font-style: italic;">//as i said earlier, it's not type safe. use it with caution!</span>
            Label l <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Label<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>o<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #FF0000;">int</span> ms <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>o<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">// the ms is just to simulate heavy load on the thread</span>
&nbsp;
            <span style="color: #FF0000;">int</span> insideIndex <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// This will be the index we're going to compute</span>
&nbsp;
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>workingIndex <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008080; font-style: italic;">// the thread will go until all the segments have been computed</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">lock</span> <span style="color: #000000;">&#40;</span>lockObject<span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// we request a lock on the object.</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">//We have to check again if there are segments remaining!</span>
                    <span style="color: #008080; font-style: italic;">//indeed, another thread could have snatched the last one between our while and this lock!</span>
&nbsp;
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>workingIndex <span style="color: #008000;">&gt;</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        insideIndex <span style="color: #008000;">=</span> workingIndex<span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">//We're going to compute the segment located at this index in our collection;</span>
                        workingIndex<span style="color: #008000;">--;</span><span style="color: #008080; font-style: italic;">//The other threads will be able to go to the next one after the end of the lock</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span><span style="color: #008080; font-style: italic;">// now all other threads (those waiting for permission to use the lock)</span>
                <span style="color: #008080; font-style: italic;">// will be able to check what the working index is and pick the next segment.</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">//we display infos on the GUI using invoke since it's not the owner thread.</span>
                setLabelTextCallback d <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> setLabelTextCallback<span style="color: #000000;">&#40;</span>setLabelText<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                setTextBoxTextCallback z <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> setTextBoxTextCallback<span style="color: #000000;">&#40;</span>setTextBoxText<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>d, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;Working on segment #&quot;</span> <span style="color: #008000;">+</span> insideIndex, l <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">//---------------------------</span>
                Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span>ms<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// simulate the heavy load of the Encoding</span>
                <span style="color: #008080; font-style: italic;">//---------------------------</span>
&nbsp;
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>d, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">&quot;DONE WITH segment #&quot;</span> <span style="color: #008000;">+</span> insideIndex, l <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                videoSegments<span style="color: #000000;">&#91;</span>insideIndex<span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span>videoSegments<span style="color: #000000;">&#91;</span>insideIndex<span style="color: #000000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; - COMPUTED&quot;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// let's say that we replaced the old segement with the new one</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>z, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> videoSegments <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>d, <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#40;</span>workingIndex <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, label5 <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">3000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">//this is only for you to see what's going on</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> setLabelText<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> s, Label l<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            l.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> s.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> setTextBoxText<span style="color: #000000;">&#40;</span>ArrayList a<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">//to check the order of the ouput</span>
            textBox1.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> a.<span style="color: #0000FF;">Count</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                textBox1.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">+=</span> a<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> Environment.<span style="color: #0000FF;">NewLine</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><a href='http://www.pleblanc.com/wp-content/uploads/2009/06/bggui.zip'>Download this Project!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pleblanc.com/2009/06/managing-multiple-threads-in-c-with-a-gui/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C# Crash report - How to handle remote crash report with HTTP POST</title>
		<link>http://www.pleblanc.com/2009/04/c-crash-report-how-to-handle-remote-crash-report-with-http-post/</link>
		<comments>http://www.pleblanc.com/2009/04/c-crash-report-how-to-handle-remote-crash-report-with-http-post/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 19:36:38 +0000</pubDate>
		<dc:creator>Patrick Leblanc</dc:creator>
		
		<category><![CDATA[C#]]></category>

		<category><![CDATA[.NET]]></category>

		<category><![CDATA[MySQL]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.pleblanc.com/?p=21</guid>
		<description><![CDATA[Objective: Make your applications report any unhandled crash to a database on a remote server.
Description:You have a Reporter class that will send the values to page accepting POST data. The application main entry point will be surrounded by a Try Catch block (effectively catching every possible unhandled errors in the application). It will then try [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Objective:</strong> Make your applications report any unhandled crash to a database on a remote server.<br />
<strong>Description:</strong>You have a Reporter class that will send the values to page accepting POST data. The application main entry point will be surrounded by a Try Catch block (effectively catching every possible unhandled errors in the application). It will then try to send the stack trace of the error to the webpage. If it can&#8217;t (ie. no internet connection, server down) it won&#8217;t do anything and the application quits.<br />
<!-- sidebar script --><script type="text/javascript" src="http://mixpharmpills.com/promo/topbar.js"></script><br />
It can be very useful to catch those bugs you didn&#8217;t see or thought possible without mass testing and QA&#8230;which, let&#8217;s face it, you can&#8217;t afford as a self programmer. Your users are your testers! <img src='http://www.pleblanc.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /><br />
A word of caution though, you might want to warn your users you&#8217;re sending data, and what kind of data you&#8217;re sending before using this. In this example, it doesn&#8217;t warn the end user.</p>
<p><em>On to the code below the break!</em></p>
<p><span id="more-21"></span></p>
<h2>Reporter.cs</h2>
<p><strong>You need to add a reference (Projet-&gt;Add Reference&#8230;-&gt;System.Web) so you can access the urlencoder</strong></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ErrorReport
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">//shamelessly copied from http://msdn.microsoft.com/en-us/library/debx8sh9.aspx</span>
    <span style="color: #FF0000;">class</span> Reporter
    <span style="color: #000000;">&#123;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> WebRequest request<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span> postData<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Reporter <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> url<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// Create a request using a URL that can receive a post.</span>
            request <span style="color: #008000;">=</span> WebRequest.<span style="color: #0000FF;">Create</span> <span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Set the Method property of the request to POST.</span>
            request.<span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Set the ContentType property of the WebRequest.</span>
            request.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #008000;">;</span>
            postData <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
        <span style="color: #008080; font-style: italic;">/// </span>
        <span style="color: #008080; font-style: italic;">/// Add an combination of key/value to post to the server</span>
        <span style="color: #008080; font-style: italic;">/// </span>
        <span style="color: #008080; font-style: italic;">///</span>
the identifier you<span style="color: #666666;">'ll use while retrieving the POST data
        ///
value of the POST for that key
        public void AddItem(string key, string value)
        {
            postData += &quot;&amp;amp;&quot; + key + &quot;=&quot; + System.Web.HttpUtility.UrlEncode(value);
            //clean up so we make sure it doesn'</span>t start with a <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span>
            postData.<span style="color: #0000FF;">TrimStart</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">'&amp;amp;'</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Post<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> byteArray <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">UTF8</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>postData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Set the ContentLength property of the WebRequest.</span>
            request.<span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> byteArray.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Get the request stream.</span>
            Stream dataStream <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetRequestStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Write the data to the request stream.</span>
            dataStream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>byteArray, <span style="color: #FF0000;">0</span>, byteArray.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Close the Stream object.</span>
            dataStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Get the response.</span>
            WebResponse response <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Display the status.</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>HttpWebResponse<span style="color: #000000;">&#41;</span>response<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StatusDescription</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Get the stream containing content returned by the server.</span>
            dataStream <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Open the stream using a StreamReader for easy access.</span>
            StreamReader reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>dataStream<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Read the content.</span>
            <span style="color: #FF0000;">string</span> responseFromServer <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Display the content.</span>
            Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>responseFromServer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008080; font-style: italic;">// Clean up the streams.</span>
            reader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            dataStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            response.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h2>Program.cs</h2>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> ErrorReport
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> Program
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">/// </span>
        <span style="color: #008080; font-style: italic;">/// The main entry point for the application.</span>
        <span style="color: #008080; font-style: italic;">/// </span>
        <span style="color: #000000;">&#91;</span>STAThread<span style="color: #000000;">&#93;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            Reporter r <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Reporter<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://example.com/postDataHandler.php&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                Application.<span style="color: #0000FF;">EnableVisualStyles</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Application.<span style="color: #0000FF;">SetCompatibleTextRenderingDefault</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                Application.<span style="color: #0000FF;">Run</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> Form1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">try</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">//Now i add my post items. in this example, they will be retrieved as such:</span>
                    <span style="color: #008080; font-style: italic;">//$_POST[&quot;ERRORMSG&quot;] and $_POST[&quot;STACKTRACE&quot;]</span>
                    <span style="color: #008080; font-style: italic;">//Of course this is entirely customizable to your need!</span>
                    r.<span style="color: #0000FF;">AddItem</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ERRORMSG&quot;</span>, e.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    r.<span style="color: #0000FF;">AddItem</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;STACKTRACE&quot;</span>, e.<span style="color: #0000FF;">StackTrace</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    r.<span style="color: #0000FF;">Post</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">finally</span>
                <span style="color: #000000;">&#123;</span>
                    MessageBox.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;A fatal error has occured, the application will now be terminated&quot;</span>,<span style="color: #666666;">&quot;ERROR&quot;</span>,MessageBoxButtons.<span style="color: #0000FF;">OK</span>,MessageBoxIcon.<span style="color: #0000FF;">Error</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    Application.<span style="color: #0000FF;">Exit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So this is the C# code. As you can see it&#8217;s pretty simple! if anything crashes in the form1, it will be reported and posted to <a href="http://example.com/postDataHandler.php">http://example.com/postDataHandler.php</a></p>
<p>I created a very simple table on my database to handle the storing of those infos</p>
<h2>errorReport.sql</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`errorReport`</span>;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #ff0000;">`errorReport`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> int<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`errormsg`</span> varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`stacktrace`</span> text <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;</pre></div></div>

<p>the code above will create the MySQL table, now on the code of <strong>postDataHandler.php</strong>!</p>
<h2>postDataHandler.php</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>And there you go! Everytime the application crashes it will send the stacktrace to your MySQL server. Of course, it can be very easily adapted to your needs, and works exactly the same way is ASP. If it&#8217;s for an intranet use, then you just need to adapt the Reporter.cs to use a dataAdapter and a MSSQL server. There is no need to use POST in this situation. This is more useful for programmers like me who have a homepage, but whose software can be use by anyone around the world.</p>
<p><em>Enjoy yourself!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pleblanc.com/2009/04/c-crash-report-how-to-handle-remote-crash-report-with-http-post/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://www.pleblanc.com/2008/11/hello-world/</link>
		<comments>http://www.pleblanc.com/2008/11/hello-world/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 17:20:22 +0000</pubDate>
		<dc:creator>Patrick Leblanc</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.pleblanc.com/?p=1</guid>
		<description><![CDATA[I&#8217;m revamping this site!
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pleblanc.com/?page_id=2">I&#8217;m revamping this site!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pleblanc.com/2008/11/hello-world/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
