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