void ThredStart(object forWatch) { ForWatcher fw = forWatch as ForWatcher; if (fw != null) { ConsoleHelper.WriteLineGreen("开始监控: " + fw.MonitorPath); while (true) { try { DateTime beginTime = DateTime.Now; ConsoleHelper.WriteLineYellow(string.Format("【{0}】Start...", this.Name)); fw.Run();// 运行ForWatcher DateTime endTime = DateTime.Now; ConsoleHelper.WriteLineYellow(string.Format("【{0}】End... {1} 至 {2} 用时:{3}", this.Name, beginTime, endTime, endTime - beginTime)); } catch (Exception ex)// 输出错误,继续执行 { ConsoleHelper.WriteLineRed(string.Format("ForWatcher exception for \"{0}\":{1}" + Environment.NewLine + "{2}", this.Name, fw.MonitorPath, ex.Message)); } Thread.Sleep(10000); } } }
/// <summary> /// 启动控制台,创建一个新线程和ForWatcher,ForWatcher将会循环检查指定路径并对附件进行操作 /// </summary> /// <param name="path"></param> /// <param name="entityName"></param> /// <param name="propertyName"></param> void Play(string path, string entityName, string propertyName) { ConsoleHelper.AllocConsole(); m_thread = new Thread(new ParameterizedThreadStart(ThredStart)); m_thread.IsBackground = true; ForWatcher forWatch = new ForWatcher(path, entityName, propertyName); m_thread.Start(forWatch); }