2012-05-25 13 views
5

मेरे पास एक बहुप्रचार आवेदन है। मैं अपने फ़ंक्शन निष्पादित करते समय इसे पास करने के लिए केवल एक थ्रेड को अपने फ़ंक्शन और अन्य थ्रेड निष्पादित करना चाहता हूं। मैं यह कैसे कर सकता हूँ?सी # केवल एक थ्रेड निष्पादित

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); 
     } 
    } 

उत्तर

10

आप 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); 
     } 
    } 
} 

केवल एक धागा Monitor.TryEnter ब्लॉक में अनुमति दी जाएगी। यदि कोई धागा यहां आता है जबकि दूसरा धागा अंदर आता है, तो Monitor.TryEnterfalse देता है।

+3

लॉक पर अन्य धागे कतार नहीं होंगे? ऐसा लगता है कि ओपी –

+0

@MatthewEvans हाँ पूछ रहा है, मैं समझ गया कि * पास * क्या मतलब है। मुझे लगता है कि संपादन ठीक से संबंधित है। –

1

आप एक 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

उपयोग कर सकते हैं आप अपने धागे को एक नाम दे सकते हैं और विधि

+0

और आप इसे कैसे सिंक्रनाइज़ करते हैं? –

0

क्या इस समाधान के बारे में में नाम की जांच करें:

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() 
     } 
    } 
} 

ऐसा लगता हो Monitor से लॉक ऑब्जेक्ट के साथ आसान (सस्ता), लेकिन शायद इतना स्पष्ट नहीं है।

संबंधित मुद्दे