Use. NET Compact Framework to develop multi-threaded programs on Windows Mobile

Source: Internet
Author: User
Background

Multitasking becomes the basic function of computers, even smart devices. Windows Mobile, based on Windows Embedded CE, supports multiple tasks at the system layer, represented by multithreading and multi-process. According to the leaked documents, Windows Phone 7 Series still supports multiple tasks as usual.

 

Introduction

Although the economy has started to recover and the unemployment rate has declined in the past, work is still difficult to find, especially for Embedded and Mobile smart devices such as Windows Embedded CE and Windows Mobile. One of the many questions asked during the recent sporadic interview was the development of multithreading. Therefore, this long weekend summarizes the multi-threaded program to prepare for subsequent interviews.

 

Development Environment

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.

Start two threads, one for sending requests and the other for processing requests.

 

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.

The request thread applies for a Message and puts the request into a shared resource (messageList ). Use the lock function to lock shared resources. Put the request into the list and use autoEvent to wake up the processing thread. At the same time, the request information is displayed on the list control.

 

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 wocould 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 wowould like to use polymorphism. use different request handle function in derided classes which have the same interface (Template Methods pattern ).

The processing thread will sleep until it receives a wake-up message. The lock function is also applicable when accessing shared resources. To reduce the lock time, I prefer to use a temporary container to cache all requests first. In this example, I only print the request information to the list control. In actual use, I usually use the Template Methods mode to process requests through the polymorphism method.

 

Source code: http://files.cnblogs.com/procoder/ThreadingDemo.zip

You are welcome to make a picture. The more you take the picture, the better I change. In this way, you will be more confident in the next interview. Thank you!

 

For the Native C ++ version, refer to Windows Mobile's multi-threaded program development using Native C ++.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.