<?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>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>
	<lastBuildDate>Thu, 29 Jul 2010 17:44:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>KMZ test</title>
		<link>http://www.pleblanc.com/2010/07/kmz-test/</link>
		<comments>http://www.pleblanc.com/2010/07/kmz-test/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 20:52:44 +0000</pubDate>
		<dc:creator>Patrick Leblanc</dc:creator>
				<category><![CDATA[Roadtrip]]></category>

		<guid isPermaLink="false">http://www.pleblanc.com/?p=94</guid>
		<description><![CDATA[Here is the track:
View Larger]]></description>
			<content:encoded><![CDATA[<p>Here is the track:<br />
<br><div style="border:1px #CCC solid;padding: 3px;"><iframe width="608" height="360" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?q=http://pleblanc.com/roadtriplogs/J2.kmz&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?q=http://pleblanc.com/roadtriplogs/J2.kmz&amp;z=14" >View Larger Map</a></small></div><br></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pleblanc.com/2010/07/kmz-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a simple MVC framework</title>
		<link>http://www.pleblanc.com/2010/03/building-a-simple-mvc-framework/</link>
		<comments>http://www.pleblanc.com/2010/03/building-a-simple-mvc-framework/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 22:09:18 +0000</pubDate>
		<dc:creator>Patrick Leblanc</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.pleblanc.com/?p=53</guid>
		<description><![CDATA[Introduction
I don&#8217;t know about you,]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>I don&#8217;t know about you, but it took me a while to fully grasp the concepts of MVCs. I mean sure, it&#8217;s a <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" target="_blank">Model View Controller</a> framework and the model manages the raw data and links it to the database, the controller manages the requests to a specific module and is the translator between &#8220;Model-talk&#8221; and &#8220;View-talk&#8221; &#8211; which should be mute btw &#8211; and the View displays what the controller says to display.</p>
<p>I&#8217;ll let you check wikipedia for all the theoretical details and the thousand related frameworks.</p>
<div class="wp-caption alignleft" style="width: 310px"><a href="http://www.symfony-project.org/jobeet/1_4/Doctrine/en/04" target="_blank"><img src="http://www.symfony-project.org/images/jobeet/1_4/04/mvc.png" alt="" width="300" height="240" /></a><p class="wp-caption-text">Courtesy of the Symfony project</p></div>
<p style="text-align: left;">Meanwhile, we have tons of schema explaining to you how they work, like this one, from the Symfony framework. We&#8217;ve all seen those, but they don&#8217;t really demystify the &#8220;<strong>How do they work?</strong>&#8220;. When you look at the code, it&#8217;s MASSIVE. And it seems working like magic.</p>
<p style="text-align: left;">This is a humble attempt to explain the basis of how all those frameworks work for the curious programmer. I&#8217;ve been strongly inspired to create this by the <a href="http://framework.zend.com/" target="_blank">Zend framework</a>, <a href="http://www.symfony-project.org/" target="_blank">Symfony</a>, <a href="http://www.yiiframework.com/" target="_blank">Yii</a> and a great tutorial found <a href="http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/" target="_blank">here</a>.</p>
<p style="text-align: left;"><strong>I strongly discourage you from writing your own MVC for production purposes</strong>. Use one of the above, take time to learn it and run with it. I personally have a preference for Symfony because it uses <a href="http://www.doctrine-project.org/" target="_blank">Doctrine</a>, which is one of my favorite PHP software ever. It&#8217;s an <strong>amazing</strong> ORM.</p>
<p style="text-align: left;">That being said&#8230;I strongly encourage you to write your own MVC to satisfy your curiosity <img src='http://www.pleblanc.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p style="text-align: left;">I don&#8217;t really know where I&#8217;m going to go with this but if it proves popular, I probably will expand the whole thing, bits by bits, just for fun! If i do though, be prepared for a lot of refactoring though, since there will be many problems. I kept this as simple and straight forward as possible to keep the code distraction free.</p>
<p style="text-align: left;">This part is all about the dispatcher. How do we fetch the right controller, output the right data. Oh, and one more thing before we get started: we know nowadays SEO is the bread and butter of the web. Therefore, we will directly code for plain text URLs. We will go down the rabbit hole as a request hitting the web server, and see, step by step, what&#8217;s going on and how is it working.</p>
<h2>Let&#8217;s begin!</h2>
<p><span id="more-53"></span></p>
<h3>Configuration</h3>
<p>Software: PHP 5.2+, Apache</p>
<p>First thing to understand is: <strong>all the traffic goes through ONE file first</strong>. Then we dispatch it, according to the URL. To do so, we need nice URLs, and to have nice URLs we need <strong>mod_rewrite</strong> and to have a functional rewrite, we need our directory structure!!</p>
<p>Here is how I did it. It&#8217;s a rough mix between symfony/yii/zend, since the 3 frameworks influenced me.<br />
<a href="http://www.pleblanc.com/wp-content/uploads/2010/03/smvc-structure.png"><img class="alignnone size-full wp-image-57" title="smvc structure" src="http://www.pleblanc.com/wp-content/uploads/2010/03/smvc-structure.png" alt="" /></a><br />
Basically, the <strong>app folder</strong> contains everything that is related to your application you are working on. (in my case, a book management for example)<br />
The <strong>lib folder</strong> contains the framework, and shouldn&#8217;t be edited between projects.<br />
The <strong>public folder</strong> is the HTDOCS. The only thing visible to the outside world. To do so, we have to configure our webserver first:</p>
<p>Now configure your Apache to have your virtual domain to point to the /public directory. Here is what my virtual host file look like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">NameVirtualHost 127.0.0.1
<span style="color: #000000; font-weight: bold;">&lt;</span>VirtualHost  127.0.0.1<span style="color: #000000; font-weight: bold;">&gt;</span>
    ServerAdmin postmaster<span style="color: #000000; font-weight: bold;">@</span>dummy-host.localhost
    DocumentRoot <span style="color: #ff0000;">&quot;D:/_Dev/sites/sandbox/mvc/public&quot;</span>
    ServerName mvc.localhost
    ErrorLog <span style="color: #ff0000;">&quot;logs/sandbox.lh-error.log&quot;</span>
    CustomLog <span style="color: #ff0000;">&quot;logs/sandbox.lh-access.log&quot;</span> combined
<span style="color: #000000; font-weight: bold;">&lt;/</span>VirtualHost<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>As you can see, it directly points to the public folder, which as nothing but an .htaccess file which redirect all the traffic to the index.php. let&#8217;s see the content of those file:<br />
<strong>/public/.htaccess</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">RewriteEngine On
&nbsp;
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_FILENAME<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>-f
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_FILENAME<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>-d
RewriteRule ^<span style="color: #7a0874; font-weight: bold;">&#40;</span>.<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>$ index.php <span style="color: #7a0874; font-weight: bold;">&#91;</span>L<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Basically, a file or a directory that <em><strong>physically</strong></em> exists in the /public directory will be served. if not, we redirect to <strong>index.php</strong></p>
<h3>A visitor lands to your front page. What&#8217;s going on?</h3>
<p>The .htaccess redirects to the index page.</p>
<p><strong>/public/index.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//simplifies our life.</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DS'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">DIRECTORY_SEPARATOR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ROOT'</span><span style="color: #339933;">,</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'LIB_PATH'</span><span style="color: #339933;">,</span> ROOT <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'lib'</span> <span style="color: #339933;">.</span>DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'smvc'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'APP_PATH'</span><span style="color: #339933;">,</span> ROOT <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'app'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// validate a SEO friendly URL</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> FILTER_VALIDATE_URL<span style="color: #339933;">,</span> FILTER_FLAG_QUERY_REQUIRED<span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>LIB_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'bootstrap.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid URL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There is nothing special here. We define a couple of constants to make our life easier, we just make sure we got ourself a valid, SEO-friendly URL and if we do, we fire the <strong>bootstrap.</strong><br />
That one is the heart and soul of the MVC framework. That&#8217;s where the request is handle, so let&#8217;s go there!</p>
<p><strong>/lib/smvc/bootstrap.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* 
 * This file is the nerveous system of the entire framework
 * It loads the config files and dispatches the requests. to the appropriate
 * controller.
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * This function automagically loads the required files, according to the name.
 * This is tighly used with the dispatcher.
 * @param string $className
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$className</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span>LIB_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span>LIB_PATH  <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'controllers'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'controllers'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'models'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'models'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$className</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Class not found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> dispatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//'car' stands for Controller Action and Request</span>
    <span style="color: #000088;">$car</span> <span style="color: #339933;">=</span> Smvc_Router<span style="color: #339933;">::</span><span style="color: #004000;">route</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$cname</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$car</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$controller</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cname</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'Controller'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$action</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$car</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$car</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$triggeredController</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$controller</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggeredController</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'execute'</span><span style="color: #339933;">.</span><span style="color: #000088;">$action</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggeredController</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getViewVariables</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'views'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$cname</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #000088;">$action</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$smvc_content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">ob_end_clean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'views'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'layout.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
dispatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The function <strong>__autoload($className)</strong> is fired every time we call a class an PHP can&#8217;t find it. Obviously since we don&#8217;t <strong>include()</strong> any other file than the bootstrap, we won&#8217;t have any class loaded. So as you can see, to keep this system as light as possible, we load classes on demand only. This is crucial to keep performance independent of the size of the framework. You can have a hundred controllers and views, it shouldn&#8217;t be any much slower than if you have only a view and a controller. This function will probably be refactored in the future as it can be improved, but for now, I like its simplicity.</p>
<p>Now the <strong>dispatcher</strong>. As you can see, it&#8217;s the only function called in the scripts (for now). Everything else is class based. Once inside it, the first thing we do is analyze the URL with the <strong>router</strong>. I chose to put it in a class, it&#8217;s not really necessary at this point the function could have been included in the <strong>bootstrap</strong>.</p>
<p><strong>/lib/smvc/Smvc_Router.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* 
 * This class finds the right Controller and Action according to the passed URL
 * and Routes.
 */</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Description of Router
 *
 * @author PLeblanc
 */</span>
final <span style="color: #000000; font-weight: bold;">class</span> Smvc_Router <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> route<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>APP_PATH <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'config'</span> <span style="color: #339933;">.</span> DS <span style="color: #339933;">.</span> <span style="color: #0000ff;">'routes.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$routes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$pattern</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$controllerAndAction</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^'</span><span style="color: #339933;">.</span> <span style="color: #000088;">$pattern</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$requestString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^'</span><span style="color: #339933;">.</span><span style="color: #000088;">$pattern</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$controllerAndAction</span><span style="color: #339933;">,</span> <span style="color: #000088;">$requestString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//we should always find a route. If not, there is problem.</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'No routes were found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This file immediately fetches the routes for the application. For now we&#8217;re keeping things simple: the route is a simple Regex expression checked against the URL. If it&#8217;s found, we return the associated Controller and Action, with the remainder of the URL. Indeed, we remove the matching pieces from the URL since we assume it&#8217;s a description of a controller and an action, and consider the rest as arguments to be processed by the Controller.</p>
<p>Here is my route file:<br />
<strong>/app/config/routes.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* 
 * This is the routing files. All URLs combinaitions should be written here.
 * The order is important as it will search from top to bottom.
 */</span>
&nbsp;
<span style="color: #000088;">$routes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'\/book\/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Index'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So this means that we are looking for a aurl such as <strong>mydomain.com/book/</strong><br />
This will send to the <strong>function dispatcher()</strong> the Controller, the Action, and the rest of the url, that will be treated as argument(s).</p>
<p>From line 31 to 34 in <strong>/lib/smvc/bootstrap.php</strong>, we prepare the variables to fit a convention. This helps keeping the code tidy. This is what I arbitrarily decided:</p>
<ul>
<li>Classes files always start with a capital letter</li>
<li>Classes files are named after the class they contain</li>
<li>Controllers must have &#8220;<strong>Controller</strong>&#8221; appended to it, such as BookController.php</li>
<li>URL Actions inside controllers must start by &#8220;<strong>execute</strong>&#8220;, such as executeIndex</li>
<li>View folders and actions are have a leading capital letter too</li>
<li>I decided that &#8216;/&#8217; represent break in arguments/routes. It makes sense in most SEO situations.</li>
</ul>
<p>Then <strong>line 36</strong>, we simply create a new controller object, with the request as argument. In this case, if the path is /book/ it will match the first route and return the BookController, which is going to be instantiated.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>36
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$triggeredController</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$controller</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>What we&#8217;re effectively doing in the case of the user trying to access <strong>mydomain.com/book/hello-world/how-are-you</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$triggeredController</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$BookController</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hello-world'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'how-are-you'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//remember we explode the request on slashes ( '/' ) on line 34</span></pre></div></div>

<p>This is what the BookController looks like:<br />
<strong>/app/controllers/BookController.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Description of BookController
 *
 * @author PLeblanc
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> BookController <span style="color: #000000; font-weight: bold;">extends</span> Smvc_Controller <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myRequest</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is my request: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, extremely basic for now. We&#8217;re just storing the 1st argument in a variable that is going to be used in the view. All the variables set like &#8220;$this->myVariable&#8221; are accessible to the view. We&#8217;re getting there in a minute. Meanwhile, the astute reader noticed we are extending an parent class. Here it is:</p>
<p><strong>/lib/smvc/Smvc_Controller.php</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Description of Smvc_Controllerclass
 *
 * @author PLeblanc
 */</span>
abstract <span style="color: #000000; font-weight: bold;">class</span> Smvc_Controller <span style="color: #009900;">&#123;</span>
&nbsp;
    protected <span style="color: #000088;">$_request</span><span style="color: #339933;">;</span>
    protected <span style="color: #000088;">$_viewVariables</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request <span style="color: #339933;">=</span> <span style="color: #000088;">$request</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __set<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_viewVariables<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __get<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_viewVariables<span style="color: #009900;">&#91;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getViewVariables<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_viewVariables<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, it&#8217;s a very simple and basic container class. We are using magic getter and setter to store all variables in a array. The request, meanwhile, is being stored by the constructor, so it&#8217;s readily available inside our controller as &#8220;$this->_request&#8221;.</p>
<p>In line 37, once the object is instantiated, we call the action associated by the matching route.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$triggeredController</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'execute'</span><span style="color: #339933;">.</span><span style="color: #000088;">$action</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The above code calls <strong>/app/controllers/BookController.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myRequest</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'This is my request: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_request<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>As I said earlier on, all the variables set in the controller are then extracted in line 39. We&#8217;re doing this because the variables are then easily used in the view. Now we start the <strong>output buffer</strong>. This is because we want to have the view rendered in to a variable. We can then inject the view inside the layout file. This is a good practice because we don&#8217;t have broken HTML files (such as a traditional Header.php + Footer.php, like Wordpress is doing).</p>
<p>Here is the view used to respond to the index action:</p>
<p><strong>/app/views/Book/Index.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h1&gt;You made a request for the BOOK CONTROLLER&lt;/h1&gt;
&nbsp;
&lt;p&gt;And this is the first argument passed:&lt;/p&gt;
&nbsp;
&lt;div style=&quot;background-color: #DDF; padding: 25px;&quot;&gt;
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$myRequest</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;/div&gt;</pre></div></div>

<p>As you can see, we can use EVERY variables used in the controller directly in the view. If, in the controller, I used $this->helloWorld, I will be able to use $helloWorld in my view, thanks to the <strong>extract()</strong> line 39 of the <strong>bootstrap.php</strong>.<br />
and finally the extremely basic layout page. as you can see, the layout path is hardcoded in the bootstrap. It&#8217;s probably something we will want to change in the future, but for now, it&#8217;s doing the job.</p>
<p><strong>/app/views/layout.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;?php echo $smvc_content ?&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>So there, you have it!</p>
<p>If you try to access it now, with this URL:<strong> http://mvc.localhost/book/hello-world</strong> you&#8217;re going to have this result:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<h1>You made a request for the BOOK CONTROLLER</h1>
<p>And this is the first argument passed:</p>
<div style="background-color: #DDF; padding: 25px;">
    This is my request: hello-world</div>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
However if you&#8217;re trying any other urls that don&#8217;t start with &#8220;book&#8221;, it&#8217;s gonna throw a fatal error, since no routes have been created for them! for example: <strong> http://mvc.localhost/hello-world</strong></p>
<pre>
Fatal error: Uncaught exception 'Exception' with message 'No routes were found' in D:\_Dev\sites\sandbox\mvc\lib\smvc\Smvc_Router.class.php:27 Stack trace: #0
D:\_Dev\sites\sandbox\mvc\lib\smvc\bootstrap.php(29): Smvc_Router::route('/hello-world') #1 D:\_Dev\sites\sandbox\mvc\lib\smvc\bootstrap.php(49): dispatcher() #2
D:\_Dev\sites\sandbox\mvc\public\index.php(15): require_once('D:\_Dev\sites\s...') #3 {main} thrown in D:\_Dev\sites\sandbox\mvc\lib\smvc\Smvc_Router.class.php on line 27
</pre>
<h3>Recap</h3>
<p>It&#8217;s a lot to absorb. It&#8217;s going to take a while, but I really tried to keep things as simple as possible to begin this tutorial. Take some time to reproduce it step by step. I&#8217;ve included all the code in quotes, nothing is hidden. If you do it step by step and recode this MVC from scratch, you&#8217;ll have a much better understanding of how all this works.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pleblanc.com/2010/03/building-a-simple-mvc-framework/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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]]></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 Start method only takes 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 argument an anonymous object containing a Label and an int which is the a time interval in ms. </span>
            <span style="color: #008080; font-style: italic;">//(the interval is used to simulate heavy load.)</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 extract 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 on 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. If successful, all the other threads will wait for this to unlock.</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>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# Crash report &#8211; 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]]></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>
		<slash:comments>0</slash:comments>
		</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>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
