/// <summary> /// 任务执行完毕的操作 /// </summary> /// <param name="args">这里同样可以传入对象,用作处理</param> public void CallBackFun(string[] strarr) { int taskId = 0; if (strarr.Length > 0) { int.TryParse(strarr[0], out taskId); } try { DateTime date = DateTime.Now; Console.Write("执行时间:"); Console.WriteLine(date.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo)); Console.WriteLine(); Console.Write("********************************************************"); Console.Write("运行结束"); Console.Write("********************************************************"); Console.WriteLine(); //任务完成-执行下一次任务 string[] strArr = new string[] {}; string[] strArr2 = new string[] {}; TaskTime t = new TaskTime { fileName = fileName, dirPath = dirPath, runRate = runRate }; t.schedule(DateTime.Now.AddMilliseconds(runRate), t.ActionTask, strArr, t.CallBackFun, strArr2); } catch (Exception e) { //Console.WriteLine("添加新任务出现错误:\n{0}\n详细错误如下:\n{1}", e.Message,e.ToString()); Console.WriteLine("--error"); Console.WriteLine("\t添加新任务出现错误:\n\t{0}", e.Message); //Console.WriteLine("\t{0}", e.Message); Console.WriteLine("--error end"); } }
static void Main(string[] args) { String dirPath = ""; String fileName = ""; long runRate = 60 * 1000L; if (args == null || args.Length <= 0) { dirPath = @"M:\temp\"; fileName = "temp.txt"; Console.WriteLine("将使用:" + dirPath + fileName + "作为缓存文件,每" + (runRate / 1000) + "秒执行一次"); } else { if (args.Length == 1) { string arg = args[0]; arg = arg.ToLower(); if ("-h".Equals(arg) || "/h".Equals(arg) || "-help".Equals(arg) || "/help".Equals(arg)) { Console.WriteLine("向指定路径写入文件后删除文件,以保证硬盘驱动器(针对移动硬盘,尤其是带节电的)不关闭"); Console.WriteLine("特别是当游戏放在移动硬盘里,游戏载入到内存后,硬盘不会读取、写入,等开启了节电再次读取游戏数据的时候会造成音画不同步,如果出现此问题,那么开启此工具会解决此问题"); Console.WriteLine("dnc [filePath] [fileName] [runRate]"); Console.WriteLine("\t" + @"filePath:文件夹路径(如:Z:\temp\)"); Console.WriteLine("\t" + "fileName:文件名:(如:temp.txt)"); Console.WriteLine("\t" + "runRate:执行频率(单位:秒)"); return; } //验证文件夹路径 else if (arg.IndexOf(":") != -1 && arg.IndexOf(@"\") != -1) { //使用第一个参数 dirPath = args[0]; Console.WriteLine("将使用:" + dirPath + fileName + "作为缓存文件,每" + (runRate / 1000) + "秒执行一次"); } else { Console.WriteLine("输入的参数有有误,输入/help获取帮助"); return; } } else if (args.Length == 2) { dirPath = args[0]; //验证文件夹路径 if (dirPath.IndexOf(":") == -1 || dirPath.IndexOf(@"\") == -1) { Console.WriteLine("输入的参数有有误,输入/help获取帮助"); return; } fileName = args[1]; Console.WriteLine("将使用:" + dirPath + fileName + "作为缓存文件,每" + (runRate / 1000) + "秒执行一次"); } else if (args.Length == 3) { dirPath = args[0]; //验证文件夹路径 if (dirPath.IndexOf(":") == -1 || dirPath.IndexOf(@"\") == -1) { Console.WriteLine("输入的参数有有误,输入/help获取帮助"); return; } fileName = args[1]; runRate = 1000L; long rate = long.Parse(args[2]); runRate = rate * runRate; Console.WriteLine("将使用:" + dirPath + fileName + "作为缓存文件,每" + (runRate / 1000) + "秒执行一次"); } } Console.WriteLine("********************************************************初始化**********************************************************"); Console.WriteLine("name:dnc"); Console.WriteLine("full Name:diskNoClose"); Console.WriteLine("version:1.0"); Console.WriteLine("author:我家兔子没耳朵 - My rabbit has no ears"); Console.WriteLine("summary:向指定路径写入文件后删除文件,以保证硬盘驱动器(针对移动硬盘,尤其是带节电的)不关闭"); //创建计时器 string[] strArr = new string[] { }; string[] strArr2 = new string[] { }; TaskTime t = new TaskTime { fileName = fileName, dirPath = dirPath, runRate = runRate }; //在定时器执行结束后再按时间间隔再启动一个定时任务 //立即执行 t.schedule(DateTime.Now, t.ActionTask, strArr, t.CallBackFun, strArr2); Console.WriteLine("定时任务已启动,每" + (runRate / 1000) + "秒执行一次"); Console.WriteLine("*******************************************************初始化成功*******************************************************"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.ReadKey(); }