AutoResetEvent



        AutoResetEvent autoEvent 
= new AutoResetEvent(true);        
        
public void DowlLoadIds()
        {
            
bool f = autoEvent.Set();
            
for (int i = 0; i < 30; i++)
            {
                
//System.Threading.Thread t = new Thread(delegate() { DownIdCallBack(autoEvent); });
                
//t.Start();
                TimerCallback timerDelegate = new TimerCallback(DownIdCallBack);
                System.Threading.Timer t 
= new System.Threading.Timer(timerDelegate, autoEvent, 0200);
            }
        }

        
int i = 0;
        
private void DownIdCallBack(object o)
        {
            var autoEvent 
= (AutoResetEvent)o;
            autoEvent.WaitOne();
            
//System.Threading.Interlocked.Increment(ref i);
            i++;
            
if (i > 20)
            {
                i 
= 0;
                autoEvent.Reset();
                
//autoEvent.WaitOne();
                return;
            }
            
//Trace.WriteLine(System.DateTime.Now.ToString());
            Trace.WriteLine(i.ToString());
            autoEvent.Set();
        }

 

posted @ 2010-10-28 15:29  zyip  阅读(202)  评论(0编辑  收藏  举报