By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,761 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,761 IT Pros & Developers. It's quick & easy.

hi , I have a question regarding the usage of EventHandle.WaitAny() Method

P: 1
There is a requirement in our system.

There is one Transaction Monitoring thread which wakes up after every 30 sec and checks for the requests that didnot get responses for "2 min" and sends Timeout Responses to the caller.

There is a need to wake up this Thread upon an "event" to send Timeout responses for all the Requests.

I tried samples and this combination does not seem to work for me.

In short , The thread should wake up after every 30 sec (periodically) and also this same thread should wake up immediately when an event is set from outside.

Can we make use of WaitAny() function here???

Below is the sample that I tried:

Expand|Select|Wrap|Line Numbers
  1.  class Program
  2.     {
  3.         static EventWaitHandle _transactionMonitorThreadWakeupEvent;
  4.         static EventWaitHandle _transactionMonitorThreadSelfExitHandle;
  5.         static EventWaitHandle setEvent;
  6.         static EventWaitHandle _deviceUnpluggedInterrupt;
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             Thread eTransactionMonitorThread;
  11.  
  12.             Thread secondThread;
  13.             _transactionMonitorThreadWakeupEvent = new AutoResetEvent(false);
  14.             setEvent = new AutoResetEvent(false);
  15.             _transactionMonitorThreadSelfExitHandle = new ManualResetEvent(true);
  16.             _deviceUnpluggedInterrupt = new AutoResetEvent(false); 
  17.             eTransactionMonitorThread = new Thread(TransactionMonitorThread);
  18.             eTransactionMonitorThread.IsBackground = true;
  19.             secondThread = new Thread(Setevents); 
  20.             eTransactionMonitorThread.Start();
  21.             secondThread.Start(); 
  22.             eTransactionMonitorThread.Join(); 
  23.  
  24.         }
  25.  
  26.         static void TransactionMonitorThread()
  27.         {
  28.             //int timeOut = 0;
  29.             //while ((timeOut = AutoResetEvent.WaitAny(new WaitHandle[] { _deviceUnpluggedInterrupt }, (int)new TimeSpan(0, 0, 0, 20000).TotalMilliseconds, false)) > 0)
  30.             //{
  31.             //    Console.WriteLine("TransactionMonitorThread Thread Got up " + System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff") + "  ");
  32.             //}
  33.             Console.WriteLine("start");
  34.             //while (true)
  35.             //{
  36.             //    int timeOut = 0;
  37.             //    Console.WriteLine("waiting");
  38.             //    TimeSpan span = new TimeSpan(0, 0, 0, 0, 20000);
  39.             //    int t = (int)span.TotalMilliseconds;
  40.             //    timeOut = AutoResetEvent.WaitAny(new WaitHandle[] { _deviceUnpluggedInterrupt }, t, false);
  41.             //    if (timeOut != 0)
  42.             //    {
  43.             //        Console.WriteLine("Event from outside is set");
  44.             //    }
  45.             //    else
  46.             //    {
  47.             //        Console.WriteLine("Timeout occured");
  48.             //    }
  49.             //    Console.WriteLine("TransactionMonitorThread Thread Got up " + System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff") + "  ");
  50.             //    Console.WriteLine(timeOut);
  51.             //}
  52.             while ((_transactionMonitorThreadWakeupEvent.WaitOne(20000, false) == false) || (_deviceUnpluggedInterrupt.WaitOne(-1, false) == true))
  53.                // while ((_deviceUnpluggedInterrupt.WaitOne(-1, false) == true) || (_transactionMonitorThreadWakeupEvent.WaitOne(20000, false) == false))
  54.                 {
  55.                     Console.WriteLine("TransactionMonitorThread Thread Got up " + System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff") + "  ");
  56.                 }
  57.  
  58.         }
  59.  
  60.         static void Setevents()
  61.         {
  62.             while (setEvent.WaitOne(10000, false) == false)
  63.             {
  64.                 Console.WriteLine("Setevents Thread Got up " + System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss:ffff") + "  ");
  65.                 _deviceUnpluggedInterrupt.Set(); 
  66.             }
  67.         }
  68.     }
Nov 4 '09 #1
Share this Question
Share on Google+
1 Reply


tlhintoq
Expert 2.5K+
P: 3,525
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Nov 4 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.