using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace IOTContainer.Common { public class TimeJobs { private static TimeJobs jobs; public static TimeJobs Jobs { get { if (null == jobs) { jobs = new TimeJobs(); } return jobs; } } public Timer _timer; public void QuerySyncMachine() { if (_timer == null) { //var timer = new Timer(new TimerCallback(HttpComm.Http.QueryPrimaryMachine), null, 0, 24 * 60 * 60 * 1000); _timer = new Timer(new TimerCallback(HttpComm.Http.QueryPrimaryMachine), null, 0, 60000); } } public void StopQuerySyncMachine() { try { if (_timer != null) { _timer.Change(-1, 0); _timer.Dispose(); } } catch (Exception ex) { Log.MyLog.WriteLogFile("StopQuerySyncMachine失败:" + ex.Message, "cmdexelog"); } } private void SetTimeTask(string keyName, TimerCallback callBack, object state, ref Timer timer) { var timeConfig = ConfigurationManager.AppSettings[keyName]; var now = DateTime.Now; var nextTime = Convert.ToDateTime($"{now.ToString("yyyy-MM-dd")} {timeConfig}"); if (nextTime < now) { nextTime = Convert.ToDateTime($"{now.AddDays(1).ToString("yyyy-MM-dd")} {timeConfig}"); } var time = Convert.ToInt32(new TimeSpan(nextTime.Ticks - now.Ticks).TotalMilliseconds); var timeperoid = 24 * 60 * 60 * 1000; timer = new Timer(callBack, state, time, timeperoid); } } }