Archive

Archive for August, 2012

Asynchronous Programming in C#

I’m reading Whitepaper: Asynchrony in .NET from the main MSDN Visual Studio Asynchronous Programming

Very exciting stuff!

Here’s the sentence that sums up the significant changes:

“So instead of taking a callback as a parameter, an asynchronous method now returns a representation of the ongoing work.”

It’s also not often that you see such excitement in a MSDN Whitepaper such as this:

“At first glance the await keyword looks like it blocks the thread until the task is complete and the data is available, but it doesn’t. Instead it signs up the rest of the method as a callback on the task, and immediately returns. When the awaited task eventually completes, it will invoke that callback and thus resume the execution of the method right where it left off!”

Here’s an easy way to spawn a background thread from the thread pool:

Task<double> task = Task.Run(() => LongRunningComputation(x, y, z));

See note in Whitepaper, if using the CTP installed on .NET 4, you would use TaskEx instead.

Here’s the link to Task Parallelism (Task Parallel Library)

There are a couple of other conventions around the Task-based Asynchronous Pattern (TAP), including how to handle cancellation and progress. These are further described in the Task-based Asynchronous Pattern document included in the Async CTP.

The section “Event handlers and void returning async methods” illustrates how to convert a UI component’s event handler into a void async method. This allows the UI to continue processing (disregarding any Task as there is none in a void return), and allows the event handler itself the freedom to perform significant processing. Excellent, this has been a significant issue when developing WP7 MVVM applications.

Not only methods but also lambda expressions can be async. Among other things this allows event handlers to be anonymous. Another useful application of asynchronous lambdas is for running asynchronous work on a background thread. Task.Run has overloads that expect delegates of type Func and Func<Task> so that you can easily express this.

Categories: Uncategorized

Using WordPress [sourcecode]

I wanted to use an offline editor for my blog along with the [-sourcecode] feature of WordPress for syntax highlighting using Syntaxhighlighter.

I started with Word 2010. At first it seemed perfect, I could save my blogs locally under version control (git) and had a familiar environment. Unfortunately the [-sourcecode] attribute does not properly work, when posed to WordPress each line of the source begins with a phantom html paragraph tag. There is no way to manually edit the HTML from within Word.

I moved on the Live Writer. Two issues showed up. I could save the blog files locally but not specify their location (annoying). The second issue is that [-sourcecode] did not work at all. Live Writer does have the ability to manually edit the HTML and that will work if immediately posted to WordPress. The problem is that if you ever switch from HTML back to WYSIWYG, Live Writer mangles the [-sourcecode] attribute to the point it no longer works once posted.

I played around with BlogJet, essentially the same issue as Live Writer. Works only in HTML, switching mangles the [-sourcecode]. One issue I did not like about BlogJet is that you can not use the tab or shift-tab keys to increment/decrement the tab, a common thing with formatting source code.

It seems that nearly all the offline WYSIWYG editors will have this issue.

I toyed with manually adding Syntaxhighlighter to the site and eliminating the use of [-sourcecode]. During this process I realized that if I replace [-sourcecode] with the actual HTML that WordPress uses:

<pre class=”brush: csharp;”>

That it works! The WYSIWYG editors leave this alone, so I can switch back and forth between visual and html editing.

So I started posting some code and noticed that my C# generics which contain the <> characters were incorrectly formatted. A bit of Syntaxhighlighter research and I discovered that these characters must be manually replaced unless instead of using the <tag> you use the <textarea> tag.

I attempted this on WordPress, however the installation of Syntaxhighlighter at WordPress.com is not configured to recognize the <textarea> tag.

I’ve settled on using the online WordPress editor, it properly recognized [-sourcecode], but it is, well, an online editor (yuck). I also don’t have my blog entries under version control.

A few references:

http://en.support.wordpress.com/code/posting-source-code/

http://en.blog.wordpress.com/2009/12/02/better-source-code-posting/

http://code.google.com/p/syntaxhighlighter/wiki/Usage

Categories: Uncategorized