Windows Mobile使用.NET Compact Framework开发多线程程序

背景

多任务成为计算机哪怕是智能设备基本的功能,iPhone不支持多任务一直为使用者所鄙视。以Windows Embedded CE作为基础的Windows Mobile从系统层就支持多任务,其中表现为多线程和多进程。从泄漏的文档看,Windows Phone 7 Series 还是一如既往的支持多任务。

 

简介

虽然说经济危机过去,经济开始回暖,失业率下降,可是工作还是不太好找,特别是Windows Embedded CE和Windows Mobile等相关嵌入式和移动智能设备的工作买少见少。在最近零星的面试中问及比较多的其中一个问题是多线程的开发。因此这个long weekend把多线程的程序总结一下,为后续的面试做准备。

 

实现

开发环境

mutilthreading-net-compact-framework-1

 mutilthreading-net-compact-framework-2

 Environment: Visual Studio 2008 + .NET Compact Framework + C# 3.0 + Windows Mobile 5.0 R2 professional (VS 2008 built-in)

 

Start threads

private void StartThreading()
{
UpdateMessageList("Start threading...");
menuItem1.Text = "Stop";
started = true;

Thread handlerThread = new Thread(HanlderThreadProc); //use delegate ThreadStart to start a new handler thread
Thread requtesterThread = new Thread(RequesterThreadProc); //Start a new requester thread
handlerThread.Name = "Hanlder";
requtesterThread.Name = "Requtester";
handlerThread.Start();
requtesterThread.Start();
}

Start two threads, one is requester that is responsible to send request and the other is handler thread which is used to handle the request.

启动两个线程,一个负责发请求,一个负责处理请求。

 

Requester thread

//Requester thread
private void RequesterThreadProc()
{
int i = 0;
string messageBody = ".NET";
while (started)
{
if (i > 1000)
{
i = 0;
}

Message msg = new Message(i, messageBody);

//lock when try to access shared resource.
lock (lockObj)
{
messageList.Add(msg);
}
string s = string.Format("{0} - {1} - {2}", Thread.CurrentThread.Name, msg.ID, msg.MessageBody);
UpdateMessageList(s);
autoEvent.Set();

++i;
Thread.Sleep(500);
}
}

Instantiate a new Message object and put into the shared container “messageList”. Use lock() function to lock the shared resource and use autoEvent to wake up handler thread. At the same time, display the thread name and the Message information to list control.

请求线程负责申请请求对象(Message)然后把请求放进共享资源(messageList)。访问共享资源的时候通过lock函数来锁定。把请求放进list以后,使用autoEvent 去唤醒处理线程。在此同时把请求信息显示到list控件上。

 

Handler thread

//Handler thread
private void HanlderThreadProc()
{
while (started)
{
//Only one thread at a time can enter.Wait until it is safe to enter.
autoEvent.WaitOne();
if (!started)
{
//If the the thread should be quit, return immediately.
return;
}

//Use temp list to decrease the lock duration.
List<Message> tempMessageList = new List<Message>();

//lock when try to access shared resource.
lock (lockObj)
{
//Access shared resource, messageList in the case.
foreach (Message msg in messageList)
{
tempMessageList.Add(msg);
}
//clear up all the request inside the list.
messageList.Clear();
}

//handle the request now.
foreach (Message msg in tempMessageList)
{
string s = string.Format("{0} - {1} - {2}", Thread.CurrentThread.Name, msg.ID, msg.MessageBody);
UpdateMessageList(s);
}
}
}

Handler thread would be in sleep until get the wake up event. Use lock() function to lock the shared resource and use temporary list to store the requests to reduce the lock duration. When process the request, only display the thread name and the Message information to list control. Usually, I would like to use polymorphism. Use different request handle function in derided classes which have the same interface (Template Methods pattern).

处理线程会一直sleep直到得到唤醒消息。访问共享资源的时候同样适用lock函数加锁。为了减少锁的时间,我偏向于使用临时容器把所有请求先缓存下来,在这个例子中,仅仅把请求信息打印到list控件,在实际运用中,我通常通过多态的方法,使用Template Methods模式来处理请求。

 

源代码: https://files.cnblogs.com/procoder/ThreadingDemo.zip

欢迎大家拍板,拍的越多,我改的越好,这样我后面的面试就更有把握了,谢谢! 

 

Native C++版本请看Windows Mobile使用Native C++开发多线程程序 


 

posted @ 2010-03-09 07:06  Jake Lin  阅读(4538)  评论(26编辑  收藏  举报