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,如需转载请自行联系原作者

相关文章
|
8月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
896 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
9月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
575 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
8月前
|
开发框架 安全 .NET
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
.NET Framework是Windows平台的开发框架,包含CLR和FCL,支持多种语言开发桌面、Web应用。常用版本有3.5、4.5.2、4.8.1,系统可同时安装多个版本,确保软件兼容运行。
1896 0
Microsoft .NET Framework 3.5、4.5.2、4.8.1,适用于 Windows 版本的 .NET,Microsoft C Runtime等下载
|
9月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
682 0
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
276 4
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
318 0
Metasploit Framework 6.4.55 (macOS, Linux, Windows) - 开源渗透测试框架
|
人工智能 芯片
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
D1net阅闻|OpenAI员工疯狂暗示,内部已成功开发ASI?被曝训出GPT-5但雪藏
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
230 0
Metasploit Framework 6.4.49 (macOS, Linux, Windows) - 开源渗透测试框架
|
SQL 小程序 API
如何运用C#.NET技术快速开发一套掌上医院系统?
本方案基于C#.NET技术快速构建掌上医院系统,结合模块化开发理念与医院信息化需求。核心功能涵盖用户端的预约挂号、在线问诊、报告查询等,以及管理端的排班管理和数据统计。采用.NET Core Web API与uni-app实现前后端分离,支持跨平台小程序开发。数据库选用SQL Server 2012,并通过读写分离与索引优化提升性能。部署方案包括Windows Server与负载均衡设计,确保高可用性。同时针对API差异、数据库老化及高并发等问题制定应对措施,保障系统稳定运行。推荐使用Postman、Redgate等工具辅助开发,提升效率与质量。
594 0
|
Linux API C#
基于 .NET 开发的多功能流媒体管理控制平台
基于 .NET 开发的多功能流媒体管理控制平台
326 9