开发者

timer lock properties as read only for lock function

my function like this i will be run this function using threading.timer ,when one thread inside the locked myLockHolder then another thread can not enter into the this lock ,when the first release this lock then enter another & the number of thread will wait for execution i want only one inside the lock & only one wait to the lock thread pool & another thread dispose the working.

TimerCallback call = new TimerCallback(reconnect);
  TimeSpan dueTime = new TimeSpan(0, 0, 0, 0, 2000);
  TimeSpan interval = new TimeSpan(0, 0, 0, 0, 2000);
 timer1 = new System.Threading.Timer(call, _opcServer, dueTime, interval);
public void reconnect(object server)
        {          

            try
            {
                lock (myLockHolder)
                {                    
                    int  i;
                    int groupcnt1 = 0, cntgroup1 = 0;
                    DataSet dsgroup1, ds2;
                    DataTable grpdt1;
                    _opcServer2 = (OpcServer[])server;
                    while (g < _opcServer2.Length)
                    {
                        SrvStatus status;
                        i = _opcServer2[g].GetStatus(out status);
                        if (HRESULTS.Failed(i))
                        {
                            int j = _opcServer[g].Connect(_opcServer2[g].HostInfo.HostName, _opcServer2[g].ServerName);
                            int id1 = opcconn.getserverID(_opcServer2[g].ServerName, _opcServer2[g].HostInfo.HostName);
                            dsgroup1 = grpclass.getgroupinfo(id1);

                            if (dsgroup1.Tables[0].Rows.Count != 0 && dsgroup1 != null)
                            {
                                grpdt1 = new DataTable();
                                grpdt1 = dsgroup1.Tables[0].Copy();
                                foreach (DataRow Row in grpdt1.Rows)
                                {
                                    if (groupcnt1 < 128)
                                    {
                                        if (cntgroup1 < grpdt1.Rows.Count)
                                        {
                                            ds2 = param.开发者_如何学编程getparameter1(Convert.ToInt32(Row["groupID"]));
                                            int timerstart = (Convert.ToInt32(Row["groupID"])) - 1;
                                            if (ds2.Tables[0].Rows.Count != 0)
                                            {
                                                OPCthread(Row, timerstart, g);

                                            }
                                            groupcnt1++;
                                            cntgroup1++;
                                        }
                                    }
                                }
                            }

                        }
                        cntgroup1 = 0;
                        g++;
                    }
                    if (g == _opcServer2.Length)
                    {
                        g = 0;
                    }
                }
            }
}


Inside of you timer callback you should do two things:

1) call timer1.Adjust to prevent new callbacks from occurring

2) set a flag so that in the event that another callback occurs before you adjust the timer that callback returns immediately. You should synchronize this with a separate lock from the one that you hold while you are doing the actual work in the callback


You can use Semaphore class to reistrict access to a limited number of threads and then you can use lock to provide access to one thread out of limited threads

you can user semaphore.WaitOne method which returns bool to find out whether a thread should wait or it should continue. If it returns true you can put your lock to allow one thread to access your logic

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜