现用C#作一WinForm程序,完成服务器特定数据的操作考虑到服务器工作的特征,程序一般在凌晨以后运行这个程序运行时间也比较长一般在晚上运行程序维护人员太累打算变为定时自动运行,像瑞星一样,可以在每天中午12点自动运行杀毒不知有哪位仁兄作过类似程序,给个思路,都用那些东西?谢谢!

解决方案 »

  1.   

    //---写成服务 给你个例子 其中数据操作部分 处于安全删除:)using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading;
    using System.Data.SqlClient;
    using System.Collections;
    using System.IO;namespace AutoMsgGM
    {
        delegate void myDelegate();    public partial class AutoMsgGM : ServiceBase
        {
            event myDelegate myEvent;
            string connStr = string.Empty;
            Thread thd;        public AutoMsgGM()
            {
                InitializeComponent();            
            }        protected override void OnStart(string[] args)
            {
                // TODO: 在此处添加代码以启动服务。            IO_CreatTextFile("D:\\AutoMsglog.txt", "服务启动:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +"\r\n", true);
                connStr = "server=(local); uid=gm; pwd=gaomai008; database=GaoMai"; //"server=(local); uid=sa; pwd=sunwei008; database=GaoMai"; //System.Configuration.ConfigurationManager.AppSettings["ConnString"];
                thd = new Thread(new ThreadStart(doEvent));
                thd.IsBackground = true;
                thd.Start();
            }        /// <summary>
            /// 创建/写入文件内容
            /// </summary>
            /// <param name="FileName">文件名(默认当前目录/包含路径)</param>
            /// <param name="FileContent">文件内容</param>
            /// <param name="act">改写(false)/追加到文件尾部(true)</param>
            /// <returns>返回bool</returns>
            private bool IO_CreatTextFile(string FileName, string FileContent, bool act)
            {
                try
                {
                    StreamWriter writer1 = new StreamWriter(FileName, act, Encoding.Default);
                    writer1.Write(FileContent);
                    writer1.Close();
                }
                catch
                {
                    return false;
                }
                return true;
            }        private void SetText(string text)
            {
                // InvokeRequired required compares the thread ID of the
                // calling thread to the thread ID of the creating thread.
                // If these threads are different, it returns true.
                //if (this.txtMsgStatus.InvokeRequired)
                //{
                //    SetTextCallback d = new SetTextCallback(SetText);
                //    this.Invoke(d, new object[] { text });
                //}
                //else
                //{
                //    this.txtMsgStatus.Text += text;
                //}
                IO_CreatTextFile("D:\\AutoMsglog.txt", text, true);        }        /// <summary>
            /// 员工合同到期提醒
            /// </summary>
            void UpUserState()
            {
               //....数据操作                SetText("\r\n系统提示: 职员合同消息更新成功!\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                catch (Exception ex)
                {
                    dr.Close();
                    tran.Rollback();
                    SetText("\r\n系统错误: 职员合同消息更新错误:" + ex.Message + "\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                finally
                {
                    dr.Close();
                    conn.Close();
                    conn.Dispose();
                }
            }
            /// <summary>
            /// 采购及供货 到货提醒
            /// </summary>
            void UpCaiGou()
            {
                //....数据操作                SetText("系统提示: 合同采购消息更新成功!\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                catch (Exception ex)
                {
                    dr.Close();
                    tran.Rollback();
                    SetText("系统错误: 合同采购消息更新错误:" + ex.Message + "\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                finally
                {
                    dr.Close();
                    conn.Close();
                    conn.Dispose();
                }
            }        /// <summary>
            /// 供货收款情况提醒
            /// </summary>
            void GetMoney()
            {
               //....数据操作                SetText("系统提示: 供货付款消息更新成功!\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                catch (Exception ex)
                {
                    SetText("系统错误: 供货付款消息更新错误:" + ex.Message + "\r\n");
                    SetText("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
                }
                finally
                {
                    conn.Close();
                    conn.Dispose();
                }
            }        void doEvent()
            {
            
                while (true)
                {                //DateTime now = DateTime.Now;
                    int i = DateTime.Now.Hour;
                    if (i > 2 && i < 4)
                    {
                        myEvent = new myDelegate(UpUserState);
                        myEvent += new myDelegate(UpCaiGou);
                        // myEvent += new myDelegate(GetMoney);
                    }
                    //if (now.Hour == 3)
                    //{
                    //    myEventB = new myDelegate(deltemp);
                    //}
                    //if (myEventA != null) myEventA();
                    //if (myEventB != null) myEventB();
                    if (myEvent != null)
                    {
                        myEvent();
                        myEvent = null;
                    }
                   // Application.DoEvents();
                    //this.doEvent();                Thread.Sleep(6000000); //每100分钟检查一次时间
                }
            }        protected override void OnStop()
            {
                // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
                IO_CreatTextFile("D:\\AutoMsglog.txt", "服务终止:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n", true);
            }
        }
    }
      

  2.   

    http://lylblog.blog.hexun.com/6068264_d.html
      

  3.   

    hertcloud给出了两种实现方式
    一种是在死循环中执行程序,中间用  Thread.Sleep定时挂起
    一种是设置定时器的Interval,定时调用程序判断解决
    我觉得第二种可能比较灵活,方便于判断时间是在哪个具体时间段,比如周日早上就不检查之类的,而第一种就好象只能定时一个具体数值的样子那么,两者还有什么区别呢?
    借楼主的地盘、问题来问,勿怪
      

  4.   

    细路1: 如果只有单纯的数据操作,那,直接使用数据任务/计划就OK
    细路2: 如果的确还有些系统级的操作(如升级)那写在WindowService就OK
      

  5.   

    采用WMI更简单:using System;
    using System.Management;public class T
    {
    public static void Main(string[] args)
    {
    ManagementEventWatcher watcher = new ManagementEventWatcher(
    new WqlEventQuery("__InstanceModificationEvent",
    " TargetInstance ISA 'Win32_LocalTime' AND " +
    " TargetInstance.Second=0"));

    MyHandler handler = new MyHandler();
    watcher.EventArrived += new EventArrivedEventHandler(handler.Arrived);

    watcher.Start();
    System.Threading.Thread.Sleep(60000);
    watcher.Stop();
    }

    public class MyHandler
    {
    public void Arrived(object sender,EventArrivedEventArgs e)
    {
    Console.WriteLine("时间到达,可执行任务!");
    }
    }
    }我这里程序的意思是当秒为0时,执行任务.具体设置可以参照如下;
    SELECT * FROM __InstanceModificationEvent
    WHERE
        TargetInstance ISA "Win32_LocalTime" 
        AND TargetInstance.Year = 2001 
        AND TargetInstance.DayOfWeek=5 
        AND TargetInstance.Hour=8 //可以把这里设置为12点
        AND TargetInstance.Minute=0 
        AND TargetInstance.Second=0运行结果:
    E:\>T1
    时间到达,可执行任务!
    ^C
    E:\>
      

  6.   

    你可以将System.Threading.Thread.Sleep(60000);改为:System.Console.ReadLine()观察.
    可以发现每当秒为0时,它将自动调用程序.同时由于是从线程池上返回通知,所以我们可以执行其他一些任务,而不去理会它.
      

  7.   

    提供另一种办法
    1、首先把Winform程序改成,运行完成后自动关闭
    2、使用Windows操作系统本身的计划任务,定期运行该EXE
      

  8.   

    .NET技术交流群(上海站):36840133
      

  9.   

    windows自带的“任务计划”可以足可以满足了