自动运行问题

田老狮 2007-03-09 11:05:43
现用C#作一WinForm程序,完成服务器特定数据的操作

考虑到服务器工作的特征,程序一般在凌晨以后运行

这个程序运行时间也比较长

一般在晚上运行程序维护人员太累

打算变为定时自动运行,像瑞星一样,可以在每天中午12点自动运行杀毒

不知有哪位仁兄作过类似程序,给个思路,都用那些东西?谢谢!
...全文
799 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
honkerhero 2007-03-12
  • 打赏
  • 举报
回复
服务器上死循环,找死
你们真是做企业级开发的么?
外包开发 2007-03-11
  • 打赏
  • 举报
回复
.NET技术交流群(上海站):36840133
sanniko 2007-03-11
  • 打赏
  • 举报
回复
提供另一种办法
1、首先把Winform程序改成,运行完成后自动关闭
2、使用Windows操作系统本身的计划任务,定期运行该EXE
ruan_hg 2007-03-11
  • 打赏
  • 举报
回复
windows自带的“任务计划”可以足可以满足了
llyzcy 2007-03-10
  • 打赏
  • 举报
回复
UP
showlin 2007-03-10
  • 打赏
  • 举报
回复
hertcloud给出了两种实现方式
一种是在死循环中执行程序,中间用 Thread.Sleep定时挂起
一种是设置定时器的Interval,定时调用程序判断解决
我觉得第二种可能比较灵活,方便于判断时间是在哪个具体时间段,比如周日早上就不检查之类的,而第一种就好象只能定时一个具体数值的样子

那么,两者还有什么区别呢?
借楼主的地盘、问题来问,勿怪
yuzl32 2007-03-10
  • 打赏
  • 举报
回复
你可以将System.Threading.Thread.Sleep(60000);改为:System.Console.ReadLine()观察.
可以发现每当秒为0时,它将自动调用程序.同时由于是从线程池上返回通知,所以我们可以执行其他一些任务,而不去理会它.
yuzl32 2007-03-10
  • 打赏
  • 举报
回复
采用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:\>
jinanjiang 2007-03-10
  • 打赏
  • 举报
回复
北京的雾霾天 2007-03-10
  • 打赏
  • 举报
回复
添加计划任务来做.
csShooter 2007-03-10
  • 打赏
  • 举报
回复
细路1: 如果只有单纯的数据操作,那,直接使用数据任务/计划就OK
细路2: 如果的确还有些系统级的操作(如升级)那写在WindowService就OK
yanzimywife_2005 2007-03-10
  • 打赏
  • 举报
回复
学习
sunrobust 2007-03-09
  • 打赏
  • 举报
回复
学习帮顶
hertcloud 2007-03-09
  • 打赏
  • 举报
回复
http://lylblog.blog.hexun.com/6068264_d.html
hertcloud 2007-03-09
  • 打赏
  • 举报
回复
//---写成服务 给你个例子 其中数据操作部分 处于安全删除:)

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);
}
}
}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧