在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
编写windows 计划任务只需要在普通的类里面使用main方法就好了,因为任务计划在创建后走的是程序的主方法,代码如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TestTask { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> static void Main(string[] args) { //其中args就是任务计划中的可添加参数,多个参数使用空格分开。 FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); foreach (var item in args) { sw.WriteLine("WindowsService(Thread): Write" + item + "\n"); } sw.Flush(); sw.Close(); fs.Close(); Class1 c = new Class1(); //Thread thread = new Thread(c.OnStart); //thread.Start(); c.OnStart(); c.OnStop(); } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestTask { public class Class1 { public void OnStart() { //实现方法 FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine("WindowsService(Thread): Service Started" + DateTime.Now.ToString() + "\n"); sw.Flush(); sw.Close(); fs.Close(); } public void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 FileStream fs = new FileStream(@"d:\xx.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine("WindowsService(Thread): Service Stopped" + DateTime.Now.ToString() + "\n"); sw.Flush(); sw.Close(); fs.Close(); } } } |
请发表评论