zoukankan      html  css  js  c++  java
  • C# windows 计划任务 程序编写

    编写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 + "
    ");
                }
                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() + "
    ");
    
                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() + "
    ");
                sw.Flush();
                sw.Close();
                fs.Close();
            }
        }
    }

  • 相关阅读:
    info plist各个功能的详解
    打个测试包弄成连接供别人下载测试
    封装的数据请求加上风火轮的效果
    福大软工 · BETA 版冲刺前准备(团队)
    福大软工 · 第十一次作业
    Alpha 冲刺 (10/10)
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
  • 原文地址:https://www.cnblogs.com/llcdbk/p/7503815.html
Copyright © 2011-2022 走看看