2012-10-06 5 views
-3

Я некоторое время работал над некоторым кодом. И у меня был вопрос:C#: концепция Thread и Multi Thread

Что такое «Thread» и «Multi Thread» означает в C#? Какова роль потоков?

+2

То очень общий и фундаментальный вопрос. Найдите это в Google, и я уверен, что вы получите много информации об этом. – Luftwaffe

+0

На этот вопрос было так много раз ответило так много разных способов, пожалуйста, попробуйте сначала его. –

+0

Читать все об этом: http://www.albahari.com/threading/ – Tudor

ответ

1

Поток представляет собой последовательность инструкций, выполняемых в контексте процесса. MultiThreading достигается, когда программа использует несколько потоков выполнения, позволяя каждому потоку совместно использовать процессор одновременно в зависимости от приоритета, назначенного этим потокам.

Вы можете передать этот код для обучения резьб

using System; 
using System.Threading; 

public class Alpha 
{ 

    // This method that will be called when the thread is started 
    public void Beta() 
    { 
     while (true) 
     { 
     Console.WriteLine("Alpha.Beta is running in its own thread."); 
     } 
    } 
}; 

public class Simple 
{ 
    public static int Main() 
    { 
     Console.WriteLine("Thread Start/Stop/Join Sample"); 

     Alpha oAlpha = new Alpha(); 

     // Create the thread object, passing in the Alpha.Beta method 
     // via a ThreadStart delegate. This does not start the thread. 
     Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); 

     // Start the thread 
     oThread.Start(); 

     // Spin for a while waiting for the started thread to become 
     // alive: 
     while (!oThread.IsAlive); 

     // Put the Main thread to sleep for 1 millisecond to allow oThread 
     // to do some work: 
     Thread.Sleep(1); 

     // Request that oThread be stopped 
     oThread.Abort(); 

     // Wait until oThread finishes. Join also has overloads 
     // that take a millisecond interval or a TimeSpan object. 
     oThread.Join(); 

     Console.WriteLine(); 
     Console.WriteLine("Alpha.Beta has finished"); 

     try 
     { 
     Console.WriteLine("Try to restart the Alpha.Beta thread"); 
     oThread.Start(); 
     } 
     catch (ThreadStateException) 
     { 
     Console.Write("ThreadStateException trying to restart Alpha.Beta. "); 
     Console.WriteLine("Expected since aborted threads cannot be restarted."); 
     } 
     return 0; 
    } 
}