Use lambda expressions in C# to simplify the parallelized code II

By Gastón C. Hillar (11 posts) on May 19, 2009 at 9:55 am

In my previous post “Use lambda expressions in C# to simplify the parallelized code” I began talking about the advantages of using lambda expressions in parallelized code.

I used a very simple example. Now, I’m going to use a more complex example to convince you that lambda expressions and parallelized code are good friends. In this post, I’m going to forget about the Task Parallel Library Beta 1 because I’m going to use a C# example for Silverlight.

You have a Silverlight application showing a simple Button through XAML code, btnStartGame. If you want to change its properties from a non-UI thread, you have to queue commands in the UI thread. You cannot change controls’ properties from other threads.

If you have to create a new thread, start it and invoke a delegate to update the UI inside it, the code could be as complex as the following lines:

// The event handler that starts the thread
private void btnStartGame_Click(object sender, RoutedEventArgs e)
{
Thread thread = new Thread(new ThreadStart(InvokeThread));
thread.Start();
}

// The code that will run in a new thread
void InvokeThread()
{
btnStartGame.Dispatcher.BeginInvoke(UpdateContent);
}

// The code that will run in the UI thread to update the text shown in the Button
void UpdateContent()
{
btnStartGame.Content = "New title.";
}

The code is indeed complex. It contains too many lines and three methods.

Now, let’s take a look at the same code, but using lambda expressions:

private void btnStartGame_Click(object sender, RoutedEventArgs e)
{
new Thread(() =>
{
btnStartGame.Dispatcher.BeginInvoke(() => btnStartGame.Content = " New title.");
}).Start();
}

The code is indeed simpler. Just a few lines and they fit in the event handler. You can replace the creation of many methods using lambda expressions. Hence, lambda expressions in C# are good friends for task-based programming.

Just a note, you have to add using System.Threading; in order to make the code run in a Silverlight 2 or Silverlight 3 Beta 1 solution.

Categories: Parallel Prog. & Multi-Core

Comments (5)

May 21, 2009 1:16 PM PDT


Eduardo Fernandez
Hi Gaston,

Firt of all, I've listened to Parallel Programming Talk with you as guest! It's great to know that there are professionals dedicated to parallel consultancy. Really interesting and very informative.

I have one question regarding this post. Does this code work with Silverlight 3 Beta 1? Can I use lambda expressions in future Silverlight 3? I'm working with C# 3 desktop apps (WinForms). However, I'm really interested in using my knowledge in Silverlight apps. Does Silverlight support multithreading?

By the way, I'm reading your book :)

Cheers,

Eduardo Fernandez
May 21, 2009 3:09 PM PDT

Gastón C. Hillar
Total Points:
3,056
Status Points:
3,056
Black Belt
Hey Eduardo,

I really enjoyed talking with Aaron and Clay last Tuesday. :)
I'm working with Silverlight 3 Beta 1 since it was available. In fact, I'm working in many projects using Silverlight 3 Beta 1 and its new capabilities.
The aforementioned code works with Silverlight 2 and with Silverlight 3 Beta 1, as explained in the post.
You'll be able to take full advantage of your existing knowledge working with Windows Forms in new Silverlight 3 apps. Silverlight 3 supports multithreading. I'll be adding a new post explaining its support.
I hope you enjoy the book. :) You'll be able to use everything you learn in Silverlight. However, take into account that Silverlight 3 will not support parallel extensions... I guess we'll have to wait for Silverligh 4.

Cheers,

Gastón

May 21, 2009 5:47 PM PDT

Gastón C. Hillar
Total Points:
3,056
Status Points:
3,056
Black Belt
Hey Eduardo,

As previously explained, your comments gave me an idea to add a new post. You can take a look at it here: http://software.intel.com/en-us/blogs/2009/05/21/silverlight.....s-using-c/
May 22, 2009 12:14 PM PDT


Erik
Hi Gaston,

I cannot find the way to use the BackgroundWorker componente in Silverlight 3 Beta 1. I cannot find it using autocomplete.
It would be awesome to use this component in Silverlight apps.

Cheers,

Erik
May 22, 2009 1:17 PM PDT

Gastón C. Hillar
Total Points:
3,056
Status Points:
3,056
Black Belt
Hey Erik,

You posted the same question on the other blog post. You'll see my comments with my answer there http://software.intel.com/en-us/blogs/2009/05/21/silverlight.....s-using-c/

Trackbacks (3)


Leave a comment  

To obtain technical support, please go to Software Support.
Name (required)*

Email (required; will not be displayed on this page)*

Your URL (optional)


Comment*