在方法 monitorCallback() 中,我将时间写入文本文件。写入文件后,我检查文件的 FileInfo 并打印它。
我得到了以下结果:
time = 16/08/2009 14:01:46, mili = 307
time = 16/08/2009 14:01:51, mili = 291
time = 16/08/2009 14:01:56, mili = 291
time = 16/08/2009 14:02:01, mili = 291
time = 16/08/2009 14:02:06, mili = 291
time = 16/08/2009 14:02:11, mili = 291
我不明白为什么时间会改变,但毫秒是固定的
private Timer monitor;
public Window1()
{
InitializeComponent();
monitor = new Timer(monitorCallback, null, 0, 5000);
}
private void monitorCallback(object state)
{
string path = @"C:\Test.txt";
Stream stream = File.OpenWrite(path);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(DateTime.Now);
writer.Close();
FileInfo fileInfo = new FileInfo(path);
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(delegate
{
Debug.WriteLine( "time = " + fileInfo.LastWriteTimeUtc + ", mili = " +
fileInfo.LastWriteTimeUtc.Millisecond);
}));
fileInfo = null;
}