Exemplo n.º 1
0
 /// <summary>
 /// Adds a new timer with the specified params. Multiple timers with different tags can be active at once. Timers are destroyed once they are stopped.
 /// </summary>
 /// <param name="duration">Duration of timer in seconds.</param>
 /// <param name="repeat">Sets the timer to loop.</param>
 /// <param name="tag">The name of the timer.</param>
 /// <returns></returns>
 public bool StartTimer(float duration, bool repeat, string tag)
 {
     if(!timers.ContainsKey(tag))
     {
         timerInfo newTimer = new timerInfo(duration,repeat,tag);
         timers.Add(tag, newTimer);
         OnTimerEvent.Add(tag, new onTimerDelegate(voidfunc));
         return true;
     }
     return false;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="logfile"></param>
        /// <param name="log"></param>
        /// <param name="lockobj"></param>
        private static void WriteLog(string logfile, string log, object lockobj = null)
        {
            var LogPath = AppDomain.CurrentDomain.BaseDirectory + @"App_Data\Log\" + DateTime.Now.ToString("yyyyMMdd") + @"\";

            if (IsLinux)
            {
                LogPath = LogPath.Replace("\\", "/");         //Linux环境处理
            }
            if (lockobj == null)
            {
                lockobj = RequestFileLock;
            }
            if (!Directory.Exists(LogPath))
            {
                //throw new Exception("目录不存在"+ LogPath);
                Directory.CreateDirectory(LogPath);
            }

            try
            {
                lock (QueueLock)
                {
                    lock (lockobj)
                    {
                        //压入缓存的日志
                        GetValue(logs, logfile).Enqueue(log);
                    }
                }
                var dt = DateTime.Now;
                if (LastTimer != null)
                {
                    LastTimer.Dispose();
                }

                if (IsSoonWrite)
                {
                    LastTimer = new timerInfo(dt, new Timer(new TimerCallback(WriteLogTimer), dt, 1000, -1));
                }
                else
                {
                    WriteLogTimer(dt);
                }
            }
            catch (Exception ex)
            {
                Exception(ex);
            }
        }