Most developers can relate to the pain of opening new threads because we’ve all done it wrong at some point and our systems don’t give the results expected. Windows CE operating system also experiences this and Microsoft unfortunately has implemented some restrictions for Win CE Operating System. I’m going to discuss how we can use threads with higher performance and terminate threads properly.
If you are beginner in thread programming in .NET please just visit http://www.albahari.com/threading/ . Windows CE has limitations for all types of operations; because it is a subset of the main operating system. So when we implement a system with threads in windows mobile, developers and designers must be concerned about performance. A demo is shown below
1. Windows Mobile 6 SDK
2. Windows Mobile 5/6 Professional Emulator.
3. Visual Studio 2008
In this overview about threads, I can say C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.
Normally a C# programs/applications start in a single thread created automatically by the CLR and operating system (the "main" thread), and is made multi-threaded by creating additional threads.
For the demonstration we are taking a very simple scenario: display real time with a labeled tread without any interruption to main thread. For this, we create a simple GUI with two buttons, such as start thread and second button to stop the thread. Then I added a status bar to display real time with the started thread.
Now we will move to frmmain.cs code file. Here I’m going to write very basic level code to start a thread then do a few operations and then stop the started background thread (I attached my sample project with this article).
Before starting I want to use a using statement to play with the thread.
Now we can access the threads related resources in upcoming code lines. First I’ll add my code here, then I will explain.
First I’m going to declare a thread object and also a bool variable to use on code.
Now look at my above code to create an instance to my thread object and pass a method with the thread cons and then start my thread immediately.
My WorkerThread method does several operations according my code. The yellow highlighted line is a delegate for play access to my control with external thread. If you haven’t heard about delegates pleases look below.
Now I’m going to run display time until the thread will stops. The line this.Invoke(…….) updates my status bar with my delegate and invokes the method.
If the application stops the thread by a button click, I’m going to tell to my delegate with an invocation like this.
Then my control update method should be like this
Now we will view the results when I click the start button.
Now we will see how to stop a started thread smoothly.
When users click the stop button I’m setting stillrun equal to false and joining the external thread with the main thread. You may ask why we want to join the external thread and the answer is, because I clicked STOP. What will we do with the Join method in thread object? Join means I’m telling the main thread to block the calling thread until a thread terminates, while continuing to perform standard COM and Send message pumping. For more about Thread.Join(), just visit http://msdn.microsoft.com/en-us/library/system.threading.thread.join.aspx.
One more important task we want to discuss here. In the windows mobile devices, a form has close/closing events. So when I click (OK) in the top bar the application will NOT terminate, it goes to a non-active process. So this time we must check if our external thread still running or not.
This article should help to show how to handle threads in windows mobile devices without any side effects to the main thread.
In this article we focused on threads in windows mobile. When we create new threads for our external operations we must be concerned about all resource costs and memory leaks. In my opinion you MUST optimize threads created, because thread resources are expensive. The .NET framework 2.0 or latest framework has Backgroudworker class for this purpose’s so below we will give all the references related to threads and Backgroundworker threads.
My name is RRaveen, currently living and working in Singapore. Ii am highly focusing on to provide better Solutions to business problems which are commonly an organization facing, retrieve information on time using information technologies.Since i loves windows mobile technology design and implementation business applications which is based on C#.And my other specializations are Asp.net,WPF and WCF .
i have been working on many back ends which are Mysql,Sqlserver and Oracle/PL/SQL/TOAD hands on experience more than 3 and a half year.