How to Create Multithreaded Application Using the .NET Compact Framework 2.0

In .NET compact framework it is possible to create a thread and the so created reference can easily be passed on to a function. Any function can be defined as an entry point to a new thread and once the function is performed or executed, the thread can be made to terminate automatically. It is also possible to create a multithreaded application using .Net Compact Framework 2.0, and for doing so you have to select the Smart Device option and then the Pocket PC option for any of your IDE project.



In the under-mentioned multithreaded application, two threads are created and both threads are made to use methods of another class called Process1. All methods used in the Process1 class are being accessed by both the threads and are used in updating data of Process1 class. In the illustrated case, two methods are made in Process1 that will update two different counters within the loop and we will see how it can be written.

Class.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace class
{
/// Process1 class that can be accessed by multiple threads.
/// This class demonstrates the use of Monitor.
public class Process1
{
private const int Loops = 10;
private int counter1;
private int counter2;
public Process1() /// Constructor of the Process1 class.
{
counter1 = 0;
counter2 = 0;
}
public void Action1() /// Access some data that Monitor protects.
{
for (int i = 0; i < Loops; i++) // Monitor.Enter(this);
{
int Counter = counter1;
Thread.Sleep(0);
Counter++;
Thread.Sleep(0);
counter1 = Counter;
Thread.Sleep(0);
}
} // Monitor.Exit(this);

public void Action2() /// Access some data that Monitor protects.
{
for (int i = 0; i < Loops; i++) // Monitor.Enter(this);
{
int Counter = counter2;
localCounter++;
counter2 = localCounter;
}
} // Monitor.Exit(this);

public int Counter /// Return the difference in the two counter variables
{ /// that have been accessed by multiple threads.
get
{
return counter1 - counter2;
}
}
}
}

The threads are made either to call Process1-Action1 or Process1-Action2 and it is made to update the data depending upon the passed on values. Once the actions in performing the loop are over, you can make your application or the flow to return to any of the counters. Accordingly provide buttons so that any one of the threads could be invoked. Furnished below are the codes that is expected to perform the above-mentioned scenario:

form1.cs

private Processing process1;
private bool Thread1Done;
private bool Thread2Done;

button2.Enabled = false;
textBox2.Text = "Monitor sample";
process1 = new Process1();
Thread1Done = false;
Thread2Done = false;
Thread MYthread1 = new Thread(new ThreadStart(MonitorofThread1));
Thread MYthread2 = new Thread(new ThreadStart(MonitorofThread2));
MYthread1.Start();
Mythread2.Start();

private void MonitorofThread1()
{
process1.Action1();
process1.Action2();
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
Thread1Done = true;
this.Invoke(new EventHandler(ThreadsManagerDone));
}
private void MonitorofThread2()
{
process1.Function1();
process1.Function2();
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
Thread2Done = true;
this.Invoke(new EventHandler(ThreadsManagerDone));
}

private void ThreadsManagerDone(object sender, System.EventArgs e)
{
if (Thread1Done && Thread2Done)
{
textBox2.Text = "Processing result: " + process1.Counter.ToString();
button2.Enabled = true;
}
}

Events are used to indicate a thread about a triggered situation in a managed multithreaded environment.

Any application that employs multithreads can run faster, and you can have a great performance with simplified procedures. But the involved programming complications and debugging methods can make it complex and with the added complications of threads, people may find it difficult to complete the job within the specified time.

If the developed application is meant for Pocket PC then you have to ensure that the Visual Studio environment is available and enabled in the selected device. For testing successful running on a Pocket PC, you may have initially test it by connecting to an emulator and evaluate the perfect running.

You can refer to the MSDN web site and learn how to effectively create, modify and run a multithreaded application. The site can also aid you in using the codes for creating a test multithreading applications that can run on .Net Compact Framework 2.0.

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.