Tuesday, August 26, 2008

Try to post source code in an elegant manner

Elegance source code via http://formatmysourcecode.blogspot.com/ :

private void HeavyBackgroundWork()
{
Debug.WriteLine("Background thread: " + Thread.CurrentThread.ManagedThreadId);

// To simulate a time-cost work.
Thread.Sleep(1000);

synchronizationContext.Post(new SendOrPostCallback(delegate(object param)
{
// We have to access TextInfo by synchronizationContext, otherwise
// you will meet an exception.
TextInfo.Text = "HeavyBackgroundWork() has been finished.";
}),
null);

Debug.WriteLine("HeavyBackgroundWork finished.");
}


Another code formatter: http://www.manoli.net/csharpformat/.
But it seems that it's not friendly with blogspot.

[Multi-Threading] Syncing between threads by SynchronizationContext

-----------------------------------------
Download source code package for this article:

If you can't see/download the zip file, please try to use TOR. TOR is a great tool that can break the GFW and let you access any website that prohibited by GFW.
http://vidalia-project.net/download.php
-----------------------------------------

I'd like to offer a series of tech articles on Multi-Threading which is a hot topic these days. Actually, I have a tech talk weeks later. So I'm trying to introduce my learning on this topic to our group first :) Please don't hesitate to give me suggestions to improve my knowledge.


Today's topic is syncing between threads by SynchronizationContext.

As you know, from MFC, WinForm to WPF, UI element can't be accessed from a thread other than UI thread. If you try to do this, you will meet an assertion, or exception. For WPF applications, there will be two threads. One is rendering thread, the other is UI thread. Rendering thread is responsible for rendering UI elements to graphics card, while UI thread is for managing UI elements, and do application logic, etc. MSDN says that you can only access UI elements from UI thread. So, how can we delegate a task which will accessing UI element from a background thread? Since we need to update UI while doing background work, reporting progress or the completion of the work. SynchronizationContext can help us.

UI thread works on a work item queue mode. All works queued in a queue. UI thread select a work item which has the highest priority in the queue when the thread finished a work item. Because work items will be executed within UI thread, a big work item can block UI operations. So make work items small enough, don't do heavy work in it. That's why we need a background worker thread.

SynchronizationContext provides Post() and Send(). You can call them from your background thread, and delegate methods passed to them will be executed within UI thread. Delegate method passed to Post() will be executed asynchronously, while to Send(), the caller of Send() will wait until the delegate method returned.

DIG DEEPER: We can imaging the internal work of Post() or Send(), they will obtain the lock of the queue (work item queue) first, then insert the work item to the queue, and then release the lock.

Please find sample code at the end of this article. Firstly, we create a background thread in Window1() which is the initialization method of the application. We use the BeginInvoke() of the delegate type HeavyBackgroundWorkDelegate. The background thread will be created and started. AsyncCallback() is the handler of completion of background thread. Please note, it will be called from the background thread. In HeavyBackgroundWork(), the entry of background thread, we access UI element by synchronizationContext.Post().

From the debug ourput of the application, we can see, AsyncCallback is in the same thread as background, while UI thread is a different thread.

-------------------
UI thread: 10
Background thread: 6
HeavyBackgroundWork finished.
AsyncCallback is called from thread: 6
----------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;

namespace Async_SynchronizationContext
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
private delegate void HeavyBackgroundWorkDelegate();
private SynchronizationContext synchronizationContext;

private void HeavyBackgroundWork()
{
Debug.WriteLine("Background thread: " + Thread.CurrentThread.ManagedThreadId);

// To simulate a time-cost work.
Thread.Sleep(1000);

synchronizationContext.Post(new SendOrPostCallback(delegate(object param)
{
// We have to access TextInfo by synchronizationContext, otherwise
// you will meet an exception.
TextInfo.Text = "HeavyBackgroundWork() has been finished.";
}),
null);

Debug.WriteLine("HeavyBackgroundWork finished.");
}

public void AsyncCallback(IAsyncResult ar)
{
// This method will be called from HeavyBackgroundWork thread, not UI thread.
Debug.WriteLine("AsyncCallback is called from thread: " + Thread.CurrentThread.ManagedThreadId);
}

public Window1()
{
InitializeComponent();

synchronizationContext = SynchronizationContext.Current;

Debug.WriteLine("UI thread: " + Thread.CurrentThread.ManagedThreadId);
HeavyBackgroundWorkDelegate backgroundWork = new HeavyBackgroundWorkDelegate(HeavyBackgroundWork);
backgroundWork.BeginInvoke(AsyncCallback, null);
}
}
}

Monday, August 11, 2008

My public key

If you have something secrete for me, please use this key:


pub 1024D/01531EA3 2/27/2007 Shaking Sun (Sun Shaking GENUINE)
Primary key fingerprint: 9C22 753B 4BB0 1740 91F7 D933 BCB3 C63A 0153 1EA3

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.7 (MingW32) - WinPT 1.2.0

mQGiBEXjoIkRBACjQiSDX9+YKW+p2jMAF1aZmFyFU67JKJ1Q1jMnZ8yTyK0iioYL
AUsW1Gye18yn5QLlyI38KN6mtO9Zb9SElyGsBjx0gfiarD1E8pHK7whFq+ZrY5WD
wdYsnXOKtR1LE/triPCtypBZRTGZqqJnAjlycopIYX2esnj+Q7U42JPP/wCg3Bm6
B2iSJLC/NolfvkyqsuNyObED/3FSXlz87V5Ve+A53G2jzHD3xb0fFyVvrCAlVcfp
w3jqRCDpqNnhEWfRF82eymhENSDJ5LLaXGvXk/FP0i6delbzoqyyZaTa8MieI9Gi
QwtZwqH/5VtxR859qaITfEeigYaEbQYg5uQLPef26ginwdu3amKWWEt33pla8ojG
crcBBACU3YfwJNttscCNQqMt0SgKdPrYL9/9/7j7cIpxhG/Tcx1WW/QV7/5O9GoH
iwN5T77ZbQen1SIVwbpr9B8fUgJ0ZiDZ//o4YaAmrd9u5ld1EnL2wpNVo2rBNHi/
BmBy6CNiGf4BTiJChPQxKm2QXjr7FY67w6eseY5oJDVDlIAyIrQ4U2hha2luZyBT
dW4gKFN1biBTaGFraW5nIEdFTlVJTkUpIDxzdW5zaGFraW5nQGdtYWlsLmNvbT6I
YAQTEQIAIAUCReOgiQIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJELyzxjoB
Ux6jz6YAn1tpRq4xZcin2cvCC6CdblXZ29O+AKDEdiBcSZx5udSw3/X1bq7NbRoU
WbkCDQRF46CVEAgAxohTys13AkSt000PCBy17NtV5vdCChmL/hrldfRZD9kurw4K
3ixfOcDZXQRmYE1BcQabJwntNPE090zLy84hM1wRc4aNW6iC2Ap1hHI4ZOntlbYd
ElShw7IDaM6+CXEiAnjG8kGldWD4zoCCBCSmSOHrSUDoBvY1UMPiZSUw+LYFh5Pw
hAyfr8gM8vc6oKy72/CSq1SdkA8YfylU90Wbka7xJbIKmTuFzqfJ5PGQ2c9yRecH
M/iuZ6lbSamPPecqw204HMYkTr6eRAGnQkCLx1fQeR3GlXi76OWg5WOFJ6dQAT0+
dsPsWY35F9vd/DbYlCJdbV4h2Fnl7l8MlMU7wwADBQf/Ym0821Qki6IfYMZ3cWR7
3gf6ClE7WstFBtSQF0fDAGqlBxzRGeuV4x75froPl09gxar8BUsYywh+1CuMr8Ur
VgAu3bWbWaJV+QJXF/EQfYHQdblcHEZ/aJbqeTX0Ot6cTeoDYRFRcVzibmipIEwk
c8hXedMungeiJUJuSHPRcT/Sa5v1xzk76KOdYVSZ1LZzSPps0QNRDE3C4+tG1oMO
3qhbc6n3Eekq5D2OjATmXnG4xBtwYq2KqRgaKbzOrUlDI0HImk3EHFxeDkZCE8VQ
dqF4iJcUXsdAiv3J26K0JSVPh0JPKTGHk9j/GIH2v2KXEmGliXlYGPZdEQXQVk1I
vohJBBgRAgAJBQJF46CVAhsMAAoJELyzxjoBUx6je7YAn2hZnY0TvjjxKfVAwxPR
zfvL4nSWAKCurJt4XCZkztb9orVulOPkv7kzXQ==
=+pwC
-----END PGP PUBLIC KEY BLOCK-----



Hey! I'm back again

I'm happy to be back again. I am working for MS now and looking backward to the time one year ago, I was looking for a job painfully.

I almost abandoned this Google blog last year. Because I found that it can't be accessed in China. It seems that something on Google blogger offended CCP or GFW.

Is it alright now?

Monday, July 23, 2007

Job finding was proved to be diffcult

Agilent was likely to refuse my job application. It was said that I was over qualified in algorithm development, and Agilent was find someone to develop an application in life science instruments.

But, I said to myself, I want to achiev a higher sallary, I want to achieve a higher gread in society.

I believe I can!

Wednesday, April 4, 2007

Algorithm of Detection of Flaw in Image Contest

Algorithm of Detection of Flaw in Image Contest
A contest held by Japan High Precision Engineering Association was ended and it's results released. Team of FAST Shang Hai represented by Wang Zhengyao (picture left)etc. won the third in the contest.http://www.china-image.cn/xinwen/120061211104341515792133.html

Bought an ASUS DVD Writer with Light Scribe Tech

Bought an ASUS DVD Writer with Light Scribe Tech
My computer was down. So I took it to Ding Hao to get it fixed. Also I want a DVD writer. After my computer had been fixed, I started seeking a DVD writer. Because my previous writer of Pioneer was down not so long time after bouhgt, this time won't bought a Pioneer one definitely. Hia hia .... I bought an ASUS writer then. And it has Light Scribe tech. You can "burn" an image or your menu onto your DVD disk after you burn your music in it. Hia hia hia ....Isn't it interesting?