Windows Mobile使用.NET Compact Framework開發多線程程式

來源:互聯網
上載者:User
背景

多任務成為電腦哪怕是智慧型裝置基本的功能,iPhone不支援多任務一直為使用者所鄙視。以Windows Embedded CE作為基礎的Windows Mobile從系統層就支援多任務,其中表現為多線程和多進程。從泄漏的文檔看,Windows Phone 7 Series 還是一如既往的支援多任務。

 

簡介

雖然說經濟危機過去,經濟開始回暖,失業率下降,可是工作還是不太好找,特別是Windows Embedded CE和Windows Mobile等相關嵌入式和移動智慧型裝置的工作買少見少。在最近零星的面試中問及比較多的其中一個問題是多線程的開發。因此這個long weekend把多線程的程式總結一下,為後續的面試做準備。

 

實現開發環境

 

 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++開發多線程程式 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.