Is there a synchronization class that guarantee FIFO order in C#?

前端 未结 7 1550
无人共我
无人共我 2020-11-28 11:01

What is it and how to use?

I need that as I have a timer that inserts into DB every second, and I have a shared resource between timer handler and the main thread. I

相关标签:
7条回答
  • 2020-11-28 11:31

    Actually the answers are good, but I solved the problem by removing the timer and run the method (timer-handler previously) into background thread as follows

        private void InsertBasicVaraibles()
        {
             int functionStopwatch = 0;
             while(true)
             {
    
               try
               {
                 functionStopwatch = Environment.TickCount;
                 DataTablesMutex.WaitOne();//mutex for my shared resources
                 //insert into DB 
               }
               catch (Exception ex)
               {
                 //Handle            
               }
               finally            
               {                
                  DataTablesMutex.ReleaseMutex();
               }
    
               //simulate the timer tick value
               functionStopwatch = Environment.TickCount - functionStopwatch;
               int diff = INSERTION_PERIOD - functionStopwatch;
               int sleep = diff >= 0 ?  diff:0;
               Thread.Sleep(sleep);
            }
        }
    
    0 讨论(0)
提交回复
热议问题