2012-05-25 25 views
5

Tôi có một ứng dụng đa luồng. Tôi muốn chỉ có một thread để thực hiện chức năng của tôi và các chủ đề khác để vượt qua nó trong khi chức năng của tôi thực hiện. Tôi có thể làm cái này như thế nào?C# Chỉ có một chủ đề thực hiện

phương pháp của tôi là một cái gì đó như:

public void setOutput(int value) 
    { 
     try 
     { 
      GPOs gpos = reader.Config.GPO; 
      gpos[1].PortState = GPOs.GPO_PORT_STATE.TRUE; 
      gpos[2].PortState = GPOs.GPO_PORT_STATE.TRUE; 
      Thread.Sleep(WAIT); 
      gpos[1].PortState = GPOs.GPO_PORT_STATE.FALSE; 
      gpos[2].PortState = GPOs.GPO_PORT_STATE.FALSE; 
     } 
     catch (Exception ex) 
     { 
      logger.Error("An Exception occure while setting GPO to " + value + " " + ex.Message); 
     } 
    } 

Trả lời

10

Bạn có thể sử dụng một đối tượng khóa kết hợp với Monitor.TryEnter.

private Object outputLock = new Object(); 

public void setOutput(int value) 
{ 
    if Monitor.TryEnter(outputLock) 
    { 
     try 
     { 
      .... your code in here 
     } 
     finally 
     { 
      Monitor.Exit(outputLock); 
     } 
    } 
} 

Chỉ một luồng tại thời điểm sẽ được phép vào khối Monitor.TryEnter. Nếu chuỗi đến ở đây trong khi một luồng khác nằm trong, thì Monitor.TryEnter trả về false.

+3

Sẽ không có hàng đợi chủ đề khác ở khóa? Điều đó dường như không phải là những gì OP đang yêu cầu –

+0

@MatthewEvans Vâng, tôi hiểu sai ý nghĩa của * pass *. Tôi nghĩ bản chỉnh sửa sẽ phù hợp với điều đó. –

1

Bạn có thể sử dụng một Mutex

using System; 
using System.Threading; 

class Test 
{ 
    // Create a new Mutex. The creating thread does not own the 
    // Mutex. 
    private static Mutex mut = new Mutex(); 
    private const int numIterations = 1; 
    private const int numThreads = 3; 

    static void Main() 
    { 
     // Create the threads that will use the protected resource. 
     for(int i = 0; i < numThreads; i++) 
     { 
      Thread myThread = new Thread(new ThreadStart(MyThreadProc)); 
      myThread.Name = String.Format("Thread{0}", i + 1); 
      myThread.Start(); 
     } 

     // The main thread exits, but the application continues to 
     // run until all foreground threads have exited. 
    } 

    private static void MyThreadProc() 
    { 
     for(int i = 0; i < numIterations; i++) 
     { 
      UseResource(); 
     } 
    } 

    // This method represents a resource that must be synchronized 
    // so that only one thread at a time can enter. 
    private static void UseResource() 
    { 
     // Wait until it is safe to enter. 
     mut.WaitOne(); 

     Console.WriteLine("{0} has entered the protected area", 
      Thread.CurrentThread.Name); 

     // Place code to access non-reentrant resources here. 

     // Simulate some work. 
     Thread.Sleep(500); 

     Console.WriteLine("{0} is leaving the protected area\r\n", 
      Thread.CurrentThread.Name); 

     // Release the Mutex. 
     mut.ReleaseMutex(); 
    } 
} 
0

Bạn có thể đặt tên cho chủ đề của bạn và kiểm tra tên trong phương pháp

+0

Và làm thế nào để bạn đồng bộ hóa điều đó? –

0

gì về giải pháp này:

private AutoResetEvent are = new AutoResetEvent(); 

public void setOutput(int value) 
{ 
    // Do not wait (block) == wait 0ms 
    if(are.WaitOne(0)) 
    { 
     try 
     { 
      // Put your code here 
     } 
     finally 
     { 
      are.Set() 
     } 
    } 
} 

Dường như có dễ dàng hơn (rẻ hơn) so với Monitor với đối tượng khóa, nhưng có thể không rõ ràng.

Các vấn đề liên quan