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模式来处理请求。

 

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

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

 

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


 



    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2010/03/09/Multithreading-dotnet-compact-framework.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NET compact FrameWork移动开发指南 * 书名:.NET compact FrameWork移动开发指南 * 作者:颜友宁 * 来源:清华大学出版社 * 出版时间:2006年11月 * ISBN:暂无 * 定价:63元 看 书 评 论 收 藏打 分 购 买 图书版权归出版社和作者所有,CSDN & DearBook独家提供试读和连载 内容介绍: 全书共6篇。第1篇主要介绍了Windows Mobile平台、.NET Compact Framework开发工具Visual Studio 2005。第2篇主要介绍了在C/S架构的移动应用程序中,如何利用.NET Compact Framework进行GUI设计和客户端应用的编程。第3篇主要介绍了.NET全新的数据访问模型ADO.NET和XML处理技术。第4篇主要介绍了移动Web应用程序开发以及移动智能设备所支持的网络技术。第5篇主要介绍了移动应用高级功能,如P/Invoke、POOM、资源与本地化、多线程编程、性能优化和移动安全策略等的开发。第6篇从实际... 目录 * 目录 序言 * 前言 第2章 .NET Compact Framework简介 * 2.1 概述 * 2.2 公共语言运行库 * 2.3 .NET Compact Framework类库 * 2.4 .NET Compact Framework不包括的功能 * 2.5 小结 第8章 自定义控件 * 8.1 概述 * 8.2 自定义控件类型与创建方式 * 8.3 复合控件 * 8.4 扩展控件 * 8.5 自绘控件 * 8.6 自定义组件 * 8.7 小结 第11章 图形 * 11.1 概述 * 11.2 基础知识 * 11.3 绘制图形 * 11.4 绘制字符串 * 11.5 绘制图像 * 11.6 绘制图形报表 * 11.7 小结 第15章 SQL Server 服务器数据访问 * 15.1 概述 * 15.2 如何访问SQL Server数据库 * 15.3 使用SqlConnection连接数据库 * 15.4 使用SqlCommand查询数据库 * 15.5 向SQL语句传递参数 * 15.6 ExecuteReader方法 * 15.7 重载ExecuteReader方法 * 15.8 使用存储过程 * 15.9 DataSet和DataAdapter组件 * 15.10 使用事务 * 15.11 SQL错误处理 * 15.12 小结 第16章 SQL Server CE本地数据访问 * 16.1 概述 * 16.2 数据库基本操作 * 16.3 远程数据访问 * 16.4 合并复制 * 16.5 冲突 * 16.6 数据同步设计策略 * 16.7 小结 第17章 XML编程 * 17.1 概述 * 17.2 XML基础 * 17.3 DOM编程 * 17.4 通过DataSet处理XML * 17.5 XML Schema * 17.6 XML序列化 * 17.7 使用XPath表达式 * 17.8 使用XML配置文件 * 17.9 小结 第18章 注册表编程 * 18.1 概述 * 18.2 Windows Mobile注册表 * 18.3 使用注册表 * 18.4 注册表应用实例 * 18.5 注册表安全 * 18.6 小结 第19章 移动Web开发 * 19.1 概述 * 19.2 移动Web网站 * 19.3 ASP.NET移动控件 * 19.4 移动Web窗体设计 * 19.5 自定义ASP.NET移动控件 * 19.6 移动Web应用的国际化 * 19.7 移动Web网站安全性 * 19.8 小结 第23章 使用POOM * 23.1 概述 * 23.2 Pocket Outlook对象模型 * 23.3 访问POOM * 23.4 使用任务对象 * 23.5 使用联系人对象 * 23.6 使用约会对象 * 23.7 小结 第24章 多线程编程 * 24.1 概述 * 24.2 Windows CE进程和线程 * 24.3 .NET Compact Framework线程 * 24.4 多线程程序设计 * 24.5 简单的多线程示例 * 24.6 改进多线程示例 * 24.7 线程池 * 24.8 Timer类 * 24.9 线程内访问界面控件 * 24.10 死锁风险 * 24.11 线程同步 * 24.12 小结

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值