locked
Semaphore Threading RRS feed

  • Question


  • I am using semaphore threading to comapre the value of the Timespan, i am starting a thread, which calls a method in which the value of timespan is checked.Say for example that if the value of timspan is 10 seconds. Then after every 10 seconds it should return true/false.

    To make it understand more clearly, i am putting the code snippet below:

     private static Semaphore m_dispatchSemaphore;
        private static bool m_running = true;

     static void Main(string[] args)
        {
          //start a new thread for each of this program.
          Thread thread1 = new Thread(ClientAPIforlogging);
          Thread m_requestDispatcher = new Thread(RequestDispatcherThread);
    thread1.Start();     
    m_requestDispatcher.Start();
         


        } 
    private static void RequestDispatcherThread()
        {
          while (m_running)
          {
            m_dispatchSemaphore = new Semaphore(0, 1000);
          triggered = m_dispatchSemaphore.WaitOne(TimeSpan.FromMilliseconds(10000),          false);
          }
       }

    Below is the code, where i am checking the value of triggered(Bool variable), But the problem which i am facing is that after the thread is executed for the first time, it always return false. so everytime the following condition would be satisfied.

     public static void ClientAPIforlogging()

        {if (triggered == false)

         //Do something.
         m_dispatchSemaphore.Release();

        }

    Kindly advise me , how i can comare the timespan(10) value for every 10 secs, the thread is started.
    Or if there is some other way , by which i can achieve  the same.Please suggest.

    Amit
    • Moved by spacewrangler Wednesday, June 18, 2008 10:02 PM More appropriate forum
    Monday, June 16, 2008 5:55 AM

All replies

  • It sounds like you want some code to run (approx.) every 10 seconds.  How about using System.Threading.Timer?
    • Proposed as answer by Bruno Yu Friday, June 20, 2008 5:15 AM
    Thursday, June 19, 2008 3:08 AM