. Net micro frameworkThread in
March 19,200 9
Yellow winter
Http://fox23.cnblogs.com/
Summary
Next-generation embedded microframework. Net micro frameworkIt provides support for Thread Scheduling and its two predecessors (. net Framework ,. NET Compact framework), micro framework does not need to rely on the thread management service provided by the OS, because micro framework itself is a"ClassOperating System ". This article introduces the multithreading principle in micro framework and the multithreading programming of WPF in. Net micro framework.
Introduction
As a new member of the. NET family,. Net micro framework is a software architecture specially designed by Microsoft for ultra-lightweight platforms (mainly low-end 32-bit microprocessor. Its structure 1:
Figure 1.. Net micro framework architecture
. Net micro framework has only one local execution thread, which runs. Net micro framework CLR (tinyclr ). Tinyclr is a self-guided runtime environment. Like the full version of. net, tinyclr manages its own memory. Therefore, we can think that. Net micro framework does not need to rely on the operating system to provide thread and memory management services. Therefore, you can port. Net micro framework to some ARM7 processors without OS or memory management units (MMU.
Although it is only executed on a single thread, the CLR requires that the driver call "looks" Asynchronous, that is, these calls will return immediately, instead of blocking until the hardware I/O of the task is completed. This is similar to the APC (Asynchronous procedure call. The thread scheduling of tinyclr depends on the completion mode of APC. The implementation of APC depends on the implementation of the timer (timer) at the pal layer in Figure 1, and 2.
Figure 2. asynchronous calls on. NET micro framework
Micro framework Basic thread operations
The following describes several basic thread-related methods supported by. Net micro framework:
1.Join
And the full version. system. threading. the thread class provides the join method. The so-called join (merge) is used to wait for the current thread until the method of calling this thread is completed or the specified wait time is reached. We will not do much here.
2.Timer
Here is the system. Threading. Timer class. Like the full. NET Framework, its constructor provides a timercallback delegate type parameter. It tells the thread pool to come up with a thread to execute the callback function at a specific time or frequency.
3.Event
Threads in the micro framework can respond with events. For example, when accessing shared resources, you can use autoresetevent to synchronize threads through a combination of wait-set.
These basic operations are used in SDK threading routines.
(Microsoft. NET micro framework \ samples \ threading)
WPF And dispatcher
When I first came into contact with the. NET micro Framework's WPF-style UI programming model (without windows forms), I always faced many performance and security problems. Data Updates and page refreshes are common issues.
Typical micro framework applications with UIProgramThere are two logical threads, one being explicitly created by the developer to process hardware I/O. The other is implicitly created and maintained by tinyclr, which is used to process all UI operations. We call it a wpf ui thread, such as drawing UI elements, drawing controls, and forms.
To update the UI elements in the micro framework, you can use dispacther and dispatchertimer to access the UI elements in a thread-safe manner. What is dispacther? You can think of it as a message queue bound to the second thread above. The wpf ui thread keeps staring at this queue to accept various operation commands. You only need to enqueue your command, that is, the function to be executed, to this queue. This function can be used for thread-safe execution.
The following example uses a clock to show how to use Dispatcher in the micro framework. In this example, we will update the text on the screen on a separate thread. Open Visual Studio to create a micro framework windows program
First, apart from the main function, we define a text and the thread responsible for updating it:
Private Text text;
Private Thread updatethread;
Then, we need to define a delegate for this update operation. the instance of this delegate will be used to add it to the "message queue" in the wpf ui:
/// <Summary>
/// Delegate used to update text
/// </Summary>
/// <Param name = "newtext"> New text </Param>
Public Delegate Void Updatetextdelegate (string newtext );
Then we need an actual function pointed to by the delegated instance, which is very simple:
Public Void Updatetext (string newtext)
{
Text. textcontent = Newtext;
}
Next, we need to let updatethread do something, that is, to use dispatcher to complete this asynchronous thread-safe UI update:
Public Void Updatetextthread ()
{
While ( True )
{
This . Dispatcher. begininvoke ( New Updatetextdelegate (updatetext ),
New Object [] {Datetime. Now. tostring ("Hh: mm: SS")} );
// Sleep for 1 second
Thread. Sleep ( 1000 );
}
}
Finally, before returning the creatwindow method, add the followingCodeTo start the above thread:
Updatethread = New Thread ( New Threadstart (updatetextthread ));
Updatethread. Start ();
The running effect is as follows:
Note that I have used a large font for you to see clearly. For how to add a Custom font for MF, see:
Http://www.cnblogs.com/fox23/archive/2008/10/20/customized-fonts-in-net-micro-framework.html
You can also use dispatchertimer to process this periodic call method. The only thing you need to change at this time is the signature of updatetext to conform to the eventhander format:
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Public void updatetext ( Object sender, eventargs e)
{< br> text. textcontent = datetime. now. tostring ( " hh: mm: SS " );
}
It is very easy to use dispatchertimer:
Dispatchtimer = New Dispatchertimer (textview. Dispatcher );
Dispatchtimer. tick + = New Eventhandler (updatetext );
Dispatchtimer. Interval = New Timespan ( 0 , 0 , 1 );
Dispatchtimer. Start ();
Micro framework Not competent in all situations
For scenarios with high throughput and high real-time requirements (for example, a device that needs to codec and output CD sound quality for audio data streams), using micro framework is not a good choice. To meet this requirement, you can use auxiliary processors (such as DSP) to connect to micro framework devices through SPI or I2C. Let them do the heavy data processing work, and use micro framework to create a friendly UI and undertake some non-strict real-time work.
Another way is to port the micro framework to a multi-threaded real-time operating system, and then deliver the Code with high real-time performance to a high-priority thread for running.
Summary
Net micro framework will. the reliability and efficiency of net are combined with the high productivity of Visual Studio to develop applications for small devices with low prices and limited resources, it helps people use familiar Visual Studio Tools to build hosted embedded applications. From this, you can find that it is so simple and natural to write a multi-threaded program with a beautiful UI on an embedded device in the way of hosting code in OO. Maybe you don't want to go back to the Win32 or POSIX-style code. At the same time, pay attention to the scope of application of micro framework. Do not use MF to directly undertake tasks with high real-time performance and large data volume.