我在windows窗口程序里加了一个定时器,第1000毫秒执行一段程序,程序如下:
int iInterval = 3600000;
DateTime now = DateTime.Now;
DateTime time1 = new DateTime(now.Year, now.Month, now.Day, 10, 30, 0, now.Millisecond);
if( now==time1)//试过datetime.compare(now, time)也不能得到等于0的时候
{
timer1.Enabled = false;
this.Post();
timer1.Interval = iInterval; //设置每小时检测
timer1.Enabled = true;
}
我的意思就是当时间走到10:30:00的时候执行Post这个函数,然后设置定时器的间隔为一小时,但是每次到了10点半的时候都不会执行,我输出了一下,时间转为字符串后看起来都是一样的,但就是不等,还有什么办法可以准确对比两个时间吗?其实精确到秒就可以了,time1最好也是日期类型。
先谢谢大侠啊
int iInterval = 3600000;
DateTime now = DateTime.Now;
DateTime time1 = new DateTime(now.Year, now.Month, now.Day, 10, 30, 0, now.Millisecond);
if( now==time1)//试过datetime.compare(now, time)也不能得到等于0的时候
{
timer1.Enabled = false;
this.Post();
timer1.Interval = iInterval; //设置每小时检测
timer1.Enabled = true;
}
我的意思就是当时间走到10:30:00的时候执行Post这个函数,然后设置定时器的间隔为一小时,但是每次到了10点半的时候都不会执行,我输出了一下,时间转为字符串后看起来都是一样的,但就是不等,还有什么办法可以准确对比两个时间吗?其实精确到秒就可以了,time1最好也是日期类型。
先谢谢大侠啊
解决方案 »
- axWebBrowser中的Word
- 关于C#串口编程问题?
- C# winform 导成Excel
- (求助)不能把基类的地址指向派生类吗?写了段代码研究多态性,但总是报错,帮忙看看。
- 国外GIS平台比较!(arcinfo,smallworld,mapinfo,autoDesk,intergraph)
- 使用OracleDataAdapter.Fill(),出现Invalid operation. The OracleDataReader is closed.
- 求教视频转flash技术!!
- C#刷新问题 连接未打开
- C#中定义结构的问题
- 使用WLAN api中WlanGetAvailableNetworkList遇到的问题
- 大型系统,如何高效导出数据到EXCEL 或者ACCESS数据库中
- 做水晶报表左边工具栏不见了,怎么弄回来呀
DateTime timeTmp = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
将time1改为
DateTime time1 = new DateTime(now.Year, now.Month, now.Day, 10, 30, 0);
然后比较timeTmp和time1就可以了
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.ReadLine();
DateTime now = DateTime.Now;
DateTime time1 = new DateTime(now.Year, now.Month, now.Day, 10, 30, 0, now.Millisecond);
Console.WriteLine("Now:\t"+now.ToString()+" ---- "+now.Ticks.ToString()+"\n");
Console.WriteLine("time1:\t"+time1.ToString()+" ---- "+time1.Ticks.ToString()+"\n");
Console.ReadLine();
}
}
}
毫秒后面还有4位,所以now和time1不太可能相等
或者可以这样判断
now.Ticks-time1.Ticks > -10000 and now.Ticks-time1.Ticks < 10000