<?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>patspam &#187; C#</title>
	<atom:link href="http://blog.patspam.com/category/comp/c-sharp/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.patspam.com</link>
	<description>patspam patspam patspam</description>
	<lastBuildDate>Fri, 18 Jun 2010 23:44:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=8400</generator>
		<item>
		<title>Asynchronous Programming in C#</title>
		<link>http://blog.patspam.com/2006/test-2</link>
		<comments>http://blog.patspam.com/2006/test-2#comments</comments>
		<pubDate>Sun, 04 Jun 2006 06:52:04 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://patspam.com/2006/06/04/test-2/</guid>
		<description><![CDATA[Quite often when writing GUI apps, (especially ones that do things over the network), it is desirable to call a synchronous method asynchronously. When you create a delegate in C#, the compiler generated delegate derived class contains a BeginInvoke and EndInvoke functions that let you do this: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; [...]]]></description>
			<content:encoded><![CDATA[<p>Quite often when writing GUI apps, (especially ones that do things over the network), it is desirable to call a synchronous method asynchronously. When you create a delegate in C#, the compiler generated delegate derived class contains a BeginInvoke and EndInvoke functions that let you do this:</p>
<pre class="brush: csharp;">
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Runtime.Remoting.Messaging;

namespace NetAsyn
{
public partial class Form1 : Form
{
private delegate string LongOperationDelegate(string longOpInput);
private delegate void LongOperationProcessOutputDelegate(string longOpOutput);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
AsynchronousExample();
}
private void SynchronousExample()
{
WebClient wc = new WebClient();
tb_Output.Text = wc.DownloadString(tb_Input.Text);
}
private void AsynchronousExample()
{
// Create the object that we want to pass to LongOperation
string longOpInput = tb_Input.Text;

// Create delegate for method that will do the work
LongOperationDelegate MyDelegate = new LongOperationDelegate(LongOperation);

// Create callback for method that will run when work is finished
AsyncCallback longOpCallback = new AsyncCallback(LongOperationCallback);

// Start work with delegate's BeginInvoke method (LongOperation's arguements come first, then callback and state object
// NB: State object can be anything you want to pass to the callback
IAsyncResult result = MyDelegate.BeginInvoke(longOpInput, longOpCallback, null);

// We could use result to call EndInvoke here (would block until work finished) or to repeatedly poll work thread
// Since we're using callback instead, result isn't used
}
private string LongOperation(string longOpInput)
{
WebClient wc = new WebClient();
return wc.DownloadString(longOpInput);
}
private void LongOperationCallback(IAsyncResult result)
{
// This method is called by LongOperation just before the thread returns, which means it is called on a separate thread to the GUI

// Get a reference to the delegate (alternatively make the delegate global or pass it as the state object - last object of BeginInvoke)
LongOperationDelegate longOperationDelegate = (LongOperationDelegate)((AsyncResult)result).AsyncDelegate;

// Get the result of the work by calling EndInvoke
string longOpOutput = longOperationDelegate.EndInvoke(result);

// Because this method is called on a separate thread to the GUI, we have to use InvokeRequired tests to access GUI objects
LongOperationProcessOutput(longOpOutput);
}

private void LongOperationProcessOutput(string longOpOutput)
{
// Because this method is called on a separate thread to the GUI, we have to use InvokeRequired to access GUI objects
if (this.InvokeRequired)
{
// Create a delegate for this method, to execute back on the GUI thread
LongOperationProcessOutputDelegate processLongOpDelegate = new LongOperationProcessOutputDelegate(LongOperationProcessOutput);

// Invoke the delegate, passing in the output from the work thread
this.Invoke(processLongOpDelegate, longOpOutput);
}
else
{
// Do the actual processing (we are now in the GUI thread)
tb_Output.Text = longOpOutput;
}
}
}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.patspam.com/2006/test-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Port Tester in C#</title>
		<link>http://blog.patspam.com/2006/port-tester-in-c</link>
		<comments>http://blog.patspam.com/2006/port-tester-in-c#comments</comments>
		<pubDate>Thu, 01 Jun 2006 05:56:17 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://patspam.com/?p=985</guid>
		<description><![CDATA[I deal with firewalls and web services a lot at work, and a lot of times when things break (or when deploying a new service) the easiest way to diagnose problems is to fire up telnet and try connecting to the port that the web service is supposed to be listening on. If you&#8217;re in [...]]]></description>
			<content:encoded><![CDATA[<p>I deal with firewalls and web services a lot at work, and a lot of times when things break (or when deploying a new service) the easiest way to diagnose problems is to fire up telnet and try connecting to the port that the web service is supposed to be listening on. If you&#8217;re in charge of the firewall it&#8217;s also nice to be able to quickly <em>eliminate</em> the firewall as the prime suspect for a failure, eg. <em>look the port is open, the firewall isn&#8217;t blocking things!</em></p>
<p>For fun, I decided to write an app that would let me do quick port testing from a GUI. I wrote it in C# (.NET 2) since I&#8217;ve been doing lot of C# development lately.</p>
<p>This is what my PortTester looks like:<br />
<img alt="Screenshot" title="Screenshot" src="http://patspam.com/projects/PortTester/Screenshot.PNG" /></p>
<p>You can download the installer <a href="http://patspam.com/projects/PortTester/PortTester.zip">here</a> (will ask you to download .NET 2 if you don&#8217;t already have it installed).</p>
<p>Some features / implementation notes:</p>
<ul>
<li>Remembers your last Host/Port using .NET DataBinding and ApplicationSettings (a really simple (~2sec) way of making your app remember user preferences)</li>
<li>IAsyncResult and a callback delegate to make Net.Socket operations Asynchronous (so that the app doesn&#8217;t lock up while a connection times out)</li>
<li>The original goal of the app was just to check if a port was open, but once I&#8217;d started it seemed like a good idea to be able to send simple strings to the port so that you can interact with a service. For example, it&#8217;s really handy to be able to connect to port 25 on a host and try sending mail:
<ol>
<li>HELO somename</li>
<li>MAIL FROM: me@address.com</li>
<li>RCPT TO: dest@address.com</li>
<li>DATA</li>
<li>&#8230;.</li>
</ol>
<p>If you&#8217;re having trouble with relaying being blocked etc.. the above method will usually trigger a helpful message from the mail server as to why your message is getting blocked. Of course I didn&#8217;t want to write a complete telnet client, so I&#8217;m trying to resist the temptation to add extra features.. <img src='http://blog.patspam.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </li>
<li>A certain amount of intelligence as to how to respond to the Enter key being pressed. For example, if you haven&#8217;t connected yet the Enter key will trigger the Connect button. If you are connected, hitting Enter whilst in the Send textbox will send that text to the server, whereas if you&#8217;re in the Host/IP textboxes hitting Enter will drop the current connection and connect to the new Host/IP. That combined with automatic positioning of the cursor depending on Connection state means that most of the time you don&#8217;t need to use the mouse at all.</li>
<li>Phone icon from <a href="http://www.iconbuffet.com">IconBuffet</a>, one of my fav sources of free stock icons for software projects</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.patspam.com/2006/port-tester-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mandarin Vocabulator</title>
		<link>http://blog.patspam.com/2005/mandarin-vocabulator</link>
		<comments>http://blog.patspam.com/2005/mandarin-vocabulator#comments</comments>
		<pubDate>Mon, 30 May 2005 07:52:27 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Chinese]]></category>

		<guid isPermaLink="false">http://patspam.com/wordpress/?p=255</guid>
		<description><![CDATA[Intro This is a simple C# program I wrote in 2004 to help me study Chinese. How It Works You tell the Vocabulator which characters you want to practice (by selecting Week Numbers and Core/Supplementary categories) You scroll through the (optionally randomised) list. You can choose to display Characters by themselves (without the Pinyin) to [...]]]></description>
			<content:encoded><![CDATA[<p><strong><img src="http://patspam.com/projects/vocabulator/MandarinVocabulator.png" /></strong><strong>Intro</strong><br />
This is a simple C# program I wrote in 2004 to help me study Chinese.</p>
<p><strong>How It Works</strong></p>
<ul>
<li>You tell the Vocabulator which characters you want to practice (by selecting Week Numbers and Core/Supplementary categories)</li>
<li>You scroll through the (optionally randomised) list.</li>
<li>You can choose to display Characters by themselves (without the Pinyin) to test your character recognition, or alternatively view only Pinyin to practice your writing skills. (In the future I might integrate the WinXP writing pad so that you can test your characters properly &#8212; including stroke order!).</li>
<li>You can also test yourself against the clock and view your self-defined list as a slideshow.</li>
<li>I&#8217;m also thinking of adding a nice interface for adding/categorising your own characters (at the moment they&#8217;re hardwired into the source code) so that you can use the Vocabulator to revise just the &#8216;hard&#8217; characters etc..</li>
</ul>
<p><strong>Download</strong><br />
<a href="http://patspam.com/projects/vocabulator/MandarinVocabulator.exe">Save this file to your desktop</a></p>
<p><strong>Installation</strong><br />
You need to have the standard Chinese Font &#8220;simsun.ttc&#8221; installed. If you&#8217;re using Windows XP, you do this by installing &#8220;Files for East Asian Languages&#8221; in &#8220;Regional and Language Options&#8221; (through the Control Panel). For other windows varients you may have to download and install the font manually. This program is written in C# so you also need to have the latest .NET framework installed (from Micro$oft Windows Update).</p>
<p><strong>Character List</strong><br />
The following Excel Spreadsheet contains all the Chinese Characters currently included in the program: <a href="http://patspam.com/projects/vocabulator/WordList.xls">WordList.xls</a>. These thousand-odd Chinese words make up the entire content of Melbourne Univeristy&#8217;s First Year (Introductory) Chinese Course. The list was created by <a href="mailto:lpd@unimelb.edu.au">Du Liping</a> of the Melbourne Institute of Asian Languages and Societies.  Thanks Du Laoshi!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.patspam.com/2005/mandarin-vocabulator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
