Techniques For Handling MultiThread Programming C#

A couple of purposes of .NET is to help accelerate programming and to improve processing speed. One way that is done is with multithreading. To understand that, simply create a program that for has more than 1 star threading data to be handled. We see that with current technology hardware is developing and becoming powerful, computers and the mobile devices are equipped with multi-core processors capable of doing more thatn one thing at once, therefore many jobs are done in parallel. development Programs are run multithreaded. Also very large-sector telecommunications networks help to increase speed and improve transmission line signals.

In .NET the Thread class is defined in the namespace System.Threading. A program using multithreading has more than 1 thread that handles simultaneous and synchronized work using .NET.

1. For example illustrate about MultiThread

In the first example below we initialize 3 the Threads t1, t2 and t3. Corresponding to those 3 Threads will have 3 static functions, a (), b () and c (). Ta see when to initialize a Thread we pass an argument such as a function pointer. And it is evident that when a Thread starts then it will perform the assigned work. The results of the functions a (), b () and c () are printed out; the characters are defined inside it. If that is performed often then we call the 3 functions in the same order as in the following wallet example, then the results obtained will be: Print out 100 characters "+", 100 characters "-" and finally is 100 characters "o". But here we for 3 functions are the same parallel implementation, should the characters be printed out interspersed among each other.

using System;
using System.Threading;
using System.Diagnostics;
namespace ConsoleApplication2
{
    class
Program
    {
        static void Main()
        {
            Thread t1 = new Thread(new ThreadStart(a));
            Thread t2 = new Thread(new ThreadStart(b));
            Thread t3 = new Thread(new ThreadStart(c));
            t1.Start();
            t2.Start();
            t3.Start(); 
            Console.Read();
        } 
        static void a()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("+ ");
            }
        } 
        static void b()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("- ");
            }
        } 
        static void c()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("o ");
            }
        }
    }
}

1.gif

2. Some commonly used methods for handling Threads

  • Abort (): With this method after calling, the thread will turn to the steady state, and returns the attribute value ThreadState Stopped.
  • Suspend (): Thread is running if the method calls Suspend () then it will stop operating until called back by the Resume method ().
  • Sleep (): When a thread is running if Sleep is called then it will fall into the standby status for the period of time t (in milliseconds) then will continue to operate. For example: "System.Threading.Thread.Sleep (1000);". When this method is called, the thread will pause for 1000 milliseconds. I see this is a static method that can be called without any thread through an instance.
  • Join (): This is a method I found very commonly used when handling threads. We can understand the meaning of this method as follows: When a thread calls the Join the thread that will be given a priority in the system. The intent of the Join method is that it is to be executed only when the thread ends. The command has run since before it was implemented with the current thread, and does not affect anything.

3. For example, the Join method in Thread

This example is similar to the example above, except that: Thread t1, t2 calls the Join method before t3 Thread Start (). For this reason, t1 and t2 will be dealt with until the end of the t3 2 begin implementation. Looking at the results it is easy to see the "return:". The character "+", "-" in turn 2 until the end of the character "o" is in the beginning.
In addition, we see that t2 is joined before t1 for in the process of the run. When the t1 join, t2 is not running, but when they join t1, t2 pausees until t2 completes, then turn t2 and t3 are final. So if Thread 2 Thread and join the join before any higher priority.

using System;
using System.Threading;
using System.Diagnostics; 
namespace ConsoleApplication2
{
    class
Program
    {
        static void Main()
        {
            Thread t1 = new Thread(new ThreadStart(a));
            Thread t2 = new Thread(new ThreadStart(b));
            Thread t3 = new Thread(new ThreadStart(c));
            t1.Start();
            t2.Start();
            t2.Join();
            t1.Join();
            t3.Start(); 
            Console.Read();
        } 
        static void a()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("+ ");
            }
        } 
        static void b()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("- ");
            }
        } 
        static void c()
        {
            for (int i = 0; i < 100; i++)
            {
                Console.Write("o ");
            }
        }
    }
}

2.gif

4. Pass to Thread


For example, under the guidance of the thread passing through the mechanism of ParameterizedThreadStart. First we create 4 Thread 1 array and initialize turn on 1 instance of ParameterizedThreadStart. An instance is created through a function pointer call to the static function "Start (info object);". When calling, the method Start () has 1 parameter, and this parameter will be passed to the Start parameter type info object.

using System;
using System.Threading;
using System.Diagnostics; 
namespace ConsoleApplication2
{
    class
Program
    {
        static void Main()
        {
           
// Create 4 thread in array
            Thread[] arrTThread = new Thread[4];
            for (int i = 0; i < arrTThread.Length; i++)
            {
               
// Start Thread with parametter
                ParameterizedThreadStart start = new ParameterizedThreadStart(StartThead);
                arrTThread[i] = new Thread(start);
                arrTThread[i].Start(i);
            } 
           
// Join Thread
            for (int i = 0; i < arrTThread.Length; i++)
            {
                arrTThread[i].Join();
            }
            Console.WriteLine("Finish");
            Console.Read();
        } 
        static void StartThead(object info)
        {
           
// Get value from info, after convert to int
            int value = (int)info;
            Console.WriteLine(value);
        }
    }
}

3.gif

From my Site: http://microsofttech.net/lap-trinh/ky-thuat-xu-ly-multi-thread-trong-lap-trinh-csharp.html

Up Next
    Ebook Download
    View all
    Learn
    View all