Tuesday, June 15, 2010

Airline Pilot With Panic Attack

C # (multithreading) - The idea of \u200b\u200ba Wrapper Class


The parallel computing is increasingly within the reach of common programming languages. Programs that are routine or simple processing cycles that can exist and coexist independently are easily managed with parallel computing. Given my refractoriness to acquire third-party products, first because it requires you to learn the logic, which may also not be agreeable, and secondly because I have an innate desire to reinvent the wheel, I decided to learn the basics of Multithreading in C # . In fact, the language is suitable and also provides intelligent solutions to that effect. The article assumes that the reader should have the basics of programming to objects and knowledge of the mechanism of delegation in addition to the thread.

fact I was in the unfortunate position of having to increase the computing performance of an application written in C # by third parties. The calculation routine in question were sequential, nested loops, but potentially more independent. I immediately thought of a class that would manage on their own cycles, broken into n-parts according to the number of processors and at the same time allowing it to manage the state of execution.

The solution first requires the definition of a class and then made the adjustment to the calculation code. In this article I will draw up the wrapper class, called MPBox (Multi-processor box) it is a basic project for later evolve into more complex cases, but still a good starting point.

initial specifications
The class must manage:
  1. the list of parameters of the calculation procedures (eg myproc (a, b, c ...)).
  2. the start and the status of implementation of procedures (eg, each is a thread).
  3. an event and a termination state properties.


What other parameters need to manage various parameters in type and number is a big problem that I solved the thinking of centralization in ad hoc classes derived from that set out below:


public class MyParamsItem
{
  public int min = 0;
  public int max = 0;
  public string caption = "";

  public MyParamsItem(int Min, int Max, string Caption)
  {
    min = Min;
    max = Max;
caption = Caption;
}}


In practice, if the procedure myproc (a, b, c) allow parameters a, b , c, these will be integrated in an object derived MyCustomParamsItem that exposes the following properties a, b, c, min, max, and caption. The definition of the procedure will be amended in myproc (MyCustomParamsItem obj), this is the first step to abstract the calculation procedures.

different parameters -> Class MyParamsItem

Up and Running Procedures
Using the system of delegation. NET can be passed as a parameter to a method, a reference to a procedure. Fantastic! And store it somewhere this reference. Doubly Awesome!
The wrapper class exposes a method called MPBox to append references to the same procedures and parameters in an array:

public void Add (MyProc approach, MyParamsItem aoParamsItem)

here is how it should be done callback procedure:

public delegate void MyProc (MyParamsItem aoParamsItem)

queued once the procedure and parameters (parameters also define the cycle to be applied, from low to high), you are ready to start all threads desired. The system's threading. NET will operate independently assigning each thread to the processor available.

public void Start ()

Thread Start -> MPBox.Start ()


Termination
as long as the threads are executing the property is valued at Working boolean true.

while (mp.Working)

{System.Threading.Thread.Sleep (10);}


Once all the threads that run, the class generates an event:

public event ThreadCustomEvent OnFinish;
public delegate void ThreadCustomEvent (object sender, System.EventArgs e);


Example
The example here downloadable used to indicate a cycle from min to max. This cycle to the start, will be the first run on a single processor, then two, then three and so on up to n. The results of a video record the time employees.
Any idea or inspiration to improve the class and its clerical workers will be welcome.



0 comments:

Post a Comment