/// <summary> /// 主逻辑 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MainLogicLoop_Tick(object sender, EventArgs e) { TimeSpan now = new TimeSpan( DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); DateTime today = DateTime.Today; //运行时遇到日期变更,则增加新key if (!AllDays.ContainsKey(today)) { AllDays.Add(today, new MyDay()); } List <MyTimeDuration> todayTimeLine = AllDays[today].TimeLine; //如果设备状态不为1 if (MyDeviceHelper.DeviceState != 1) { if (todayTimeLine.Count == 0 || todayTimeLine.Last().TimeDurationTask.ComputerStatus != MyDeviceHelper.DeviceState) { MyTimeDuration duration = new MyTimeDuration() { StartTime = now, TimeDurationTask = new MyTask() { ComputerStatus = MyDeviceHelper.DeviceState }, StopTime = now }; todayTimeLine.Add(duration); } else { todayTimeLine.Last().StopTime = now; } AllDays[today].TimeLine = todayTimeLine; return; } IntPtr nowFocusWindow = MyWindowHelper.GetFocusWindow(); string nowFocusWindowTitle = MyWindowHelper.GetWindowTitle(nowFocusWindow); //如果今天还未记录到任务 或 切换了任务 if (todayTimeLine.Count == 0 || todayTimeLine.Last().TimeDurationTask.ApplicationTitle != nowFocusWindowTitle) { Process nowFocusProcess = MyProcessHelper.GetWindowPID(nowFocusWindow); if (nowFocusProcess.Id == 0) { return; } string nowFocusProcessName = MyProcessHelper.GetProcessName(nowFocusProcess); if (string.IsNullOrEmpty(nowFocusProcessName)) { return; } MyTask nowFocusTask = new MyTask() { ApplicationName = nowFocusProcessName, ApplicationFilePath = MyProcessHelper.GetProcessPath(nowFocusProcess), ApplicationTitle = MyWindowHelper.GetWindowTitle(nowFocusWindow), ApplicationIcon_Base64 = MyDataHelper.ImgToBase64String(MyProcessHelper.GetProcessIcon(nowFocusProcess)) }; MyTimeDuration nowTimeDuration = new MyTimeDuration() { StartTime = now, StopTime = now, TimeDurationTask = nowFocusTask }; if (todayTimeLine.Count != 0) { todayTimeLine.Last().StopTime = now; } todayTimeLine.Add(nowTimeDuration); } else { todayTimeLine.Last().StopTime = now; } AllDays[today].TimeLine = todayTimeLine; }