コード例 #1
0
        void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (Interlocked.Exchange(ref inTimer, 1) == 0)
            {
                try
                {
                    var lstModel = LstDeveiceTask.Where(t => (t.Priority == 0)).OrderByDescending(t => t.Priority).ToList(); //优先级越大越先执行
                    foreach (var model in lstModel)
                    {
                        //if (CoolFrechFlag)
                        //{
                        //    model.LastCollTime = DateTime.Now.AddSeconds(3);
                        //    CoolFrechFlag = false;

                        //}

                        if (model.LastCollTime <= DateTime.Now)
                        {
                            //Utils.Logger.Debug(string.Format("Time:{0}  ExecID:{1}  DevConnectStatus:{2}", DateTime.Now.ToString(), model.ExecID, model.DevConnectStatus.ToString()));

                            System.Threading.Thread.Sleep(100);
                            //执行设备命令
                            Task.Run(() =>
                            {
                                DeveiceTaskExecute.TaskExecuteAsync(model);
                            });

                            //执行完成在当前时间+执行频率;
                            model.LastCollTime = DateTime.Now.AddSeconds(model.CollFrequency);
                        }



                        //model.CollFrequency--;
                    }
                }
                catch (Exception ex)
                {
                    Utils.Logger.Error(ex.ToString());
                }
                finally
                {
                    Interlocked.Exchange(ref inTimer, 0);
                }
            }
        }
コード例 #2
0
        void _timer_Tick(object sender, EventArgs e)
        {
            var dt1 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            try
            {
                var lstModel = LstDeveiceTask.Where(t => (t.Priority == 0)).OrderByDescending(t => t.Priority).ToList(); //优先级越大越先执行
                foreach (var model in lstModel)
                {
                    //if (CoolFrechFlag)
                    //{
                    //    model.LastCollTime = DateTime.Now.AddSeconds(3);
                    //    CoolFrechFlag = false;

                    //}

                    //int i = DateTime.Compare(dt1, model.LastCollTime);
                    if (model.LastCollTime.ToString("yyyy-MM-dd HH:mm:ss").Equals(dt1))
                    {
                        System.Threading.Thread.Sleep(1000);
                        //执行设备命令
                        Task.Run(() =>
                        {
                            DeveiceTaskExecute.TaskExecuteAsync(model);
                        });

                        //执行完成在当前时间+执行频率;
                        model.LastCollTime = DateTime.Now.AddSeconds(3);
                    }



                    //model.CollFrequency--;
                }
            }
            catch (Exception ex)
            {
                Utils.Logger.Error(ex.ToString());
            }
        }