Esempio n. 1
0
 /// <summary>
 /// 写出运行时状态信息
 /// </summary>
 private void WriteStatus(FormActiveLogModel status)
 {
     try
     {
         if (status != null)// && Str.Ok(status.FormName, status.FormText))// && status.Duration > 0
         {
             //设置日志目录和日志文件
             string path = DirTool.Combine(LogPath, "form_active");
             string file = DirTool.Combine(path, DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
             //创建日志目录
             DirTool.Create(path);
             //写出日志
             TxtTool.Append(file, status.ToString());
         }
         Cleaner();
     }
     catch { }
 }
Esempio n. 2
0
        public bool Start()
        {
            //如果任务停止运行,则重新创建Token,并释放上次任务
            if (Listener != null && Listener.Status != TaskStatus.Running)
            {
                try
                {
                    CancelToken = new CancellationTokenSource();
                    Listener.Dispose();
                }
                catch { }
            }
            //如果任务没取消,并且没有运行任务,则运行任务
            if (!CancelToken.IsCancellationRequested && (Listener == null || Listener.Status != TaskStatus.Running))
            {
                Listener = Task.Factory.StartNew(() =>
                {
                    try
                    {
                        FormActiveLogModel active = new FormActiveLogModel();
                        while (!CancelToken.IsCancellationRequested)
                        {
                            active.EndTime = DateTime.Now;

                            Form form = Form.ActiveForm;
                            if ((form?.Name ?? "") != active.FormName)
                            {
                                WriteStatus(active);//写出数据
                                active.FormName  = form?.Name ?? "";
                                active.FormText  = form?.Text ?? "";
                                active.BeginTime = DateTime.Now;
                            }

                            Thread.Sleep(Interval);//等待间隔时间
                        }
                    }
                    catch { }
                }, CancelToken.Token);
                return(true);
            }
            return(false);
        }