Beispiel #1
0
        /// <summary>
        /// 向计时器添加一个以10毫秒为间隔的执行任务
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <param name="milliseconds_10">执行任务时间间隔,单位:10毫秒</param>
        /// <param name="whileCount">执行次数, 为零时无限执行,默认值为零</param>
        /// <param name="action">执行任务</param>
        /// <returns></returns>
        public static AsynicData AddOf10ms(string taskName, int milliseconds_10, int whileCount, Action <AsynicData> action)
        {
            AsynicElement asynicElement = null;

            dic.TryGetValue(taskName, out asynicElement);
            if (null != asynicElement)
            {
                return(asynicElement.asynicData);
            }
            sleepNum      = 10;
            asynicElement = ExecAsynic(taskName, true, 0, milliseconds_10, sleepNum, whileCount, action);
            return(asynicElement.asynicData);
        }
Beispiel #2
0
        static AsynicElement ExecAsynic(string taskName, bool isAdd, int milliseconds_100, int milliseconds_10, int UnitOfMS, int whileCount, Action <AsynicData> action)
        {
            lock (_asynicObj)
            {
                AsynicElement ae = null;
                if (string.IsNullOrEmpty(taskName))
                {
                    foreach (KeyValuePair <string, AsynicElement> item in dic)
                    {
                        if (item.Value.asynicData.isStop)
                        {
                            continue;
                        }
                        if (0 != item.Value.WhileCount && item.Value.WhileCount <= item.Value.asynicData.currentCount)
                        {
                            continue;
                        }
                        item.Value.currentMS++;
                        if (item.Value.currentMS == item.Value.Milliseconds)
                        {
                            item.Value.currentMS = 0;
                            item.Value.asynicData.currentCount++;
                            if (999999 == item.Value.asynicData.currentCount)
                            {
                                item.Value.asynicData.currentCount = 0;
                            }
                            item.Value.exec();
                        }
                    }
                }
                else if (isAdd)
                {
                    ae = new AsynicElement();
                    ae.Milliseconds  = 0 < milliseconds_100 ? milliseconds_100 : milliseconds_10;
                    ae.Milliseconds1 = ae.Milliseconds;
                    ae.UnitOfMS      = UnitOfMS;
                    ae.UnitOfMS1     = UnitOfMS;
                    ae.WhileCount    = whileCount;
                    ae.action        = action;
                    ae.TaskName      = taskName;
                    dic.Add(taskName, ae);

                    foreach (var item in dic)
                    {
                        if (sleepNum != item.Value.UnitOfMS)
                        {
                            item.Value.currentMS    = 0;
                            item.Value.Milliseconds = (item.Value.Milliseconds * item.Value.UnitOfMS) / sleepNum;
                            if (0 == item.Value.Milliseconds)
                            {
                                item.Value.Milliseconds = 1;
                            }
                            if (sleepNum == item.Value.UnitOfMS1)
                            {
                                item.Value.Milliseconds = item.Value.Milliseconds1;
                            }
                            item.Value.UnitOfMS = sleepNum;
                        }
                    }
                }
                else
                {
                    dic.TryGetValue(taskName, out ae);
                    if (null != ae)
                    {
                        dic.Remove(taskName);
                    }
                }
                return(ae);
            }
        }