自动运行问题
现用C#作一WinForm程序,完成服务器特定数据的操作
考虑到服务器工作的特征,程序一般在凌晨以后运行
这个程序运行时间也比较长
一般在晚上运行程序维护人员太累
打算变为定时自动运行,像瑞星一样,可以在每天中午12点自动运行杀毒
不知有哪位仁兄作过类似程序,给个思路,都用那些东西?谢谢!
------解决方案--------------------//---写成服务 给你个例子 其中数据操作部分 处于安全删除:)
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();