如何检查DateTime是否已初始化? [英] How do you check if DateTime is initialized?
本文介绍了如何检查DateTime是否已初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果启动Datetime,我需要帮助制作一个每秒检查一次的代码。如果它已启动,它应该关闭已经运行的功能。我知道dispatchertimer但我希望它在后台运行该线程。原因是因为我有一个直到日期时间开始播放的媒体播放器,导致电影每1秒重启一次。
如果你们能做到的话,我会非常感谢帮助我,有点绝望...
I need help with making a code that checks every second if Datetime is started. If it is started it should close the already running function. I know dispatchertimer but I want it to run the thread in background. The reason is because I have a mediaplayer that plays till datetime is started, causing the movie to restart every 1 second.
I would be very thank full if you guys could help me, a little bit desperate...
Dictionary<string, string> listBox3Dict = new Dictionary<string, string>();
private bool listbox3job()
{
AxWMPLib.AxWindowsMediaPlayer axWmp =
wfh.Child as AxWMPLib.AxWindowsMediaPlayer;
DateTime? start = DateTimePicker1.Value;
DateTime? end = DateTimePicker2.Value; // This. End date. If end date. Stop movie.
DateTime now = DateTime.Now;
if (start == null || end == null)
{
}
else if (now >= start.Value && now <= end.Value)
{
foreach (var selected in listBox3.Items)
{
string s = selected.ToString();
if (listBox3Dict.ContainsKey(s))
{
axWmp.URL = (listBox3Dict[s]);
}
}
return true;
}
return false;
}
推荐答案
您需要使用计时器 [ ^ ]
示例:
http:/ /social.msdn.microsoft.com/Forums/en-US/winforms/thread/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/ [ ^ ]
http://www.dotnetperls.com/timer [ ^ ]
http://forum.co decall.net/topic/65434-c-working-with-timers/ [ ^ ]
微秒和毫秒C#定时器 [ ^ ]
所有关于.NET计时器 - 比较 [ ^ ]
C#中的高性能计时器 [ ^ ]
You need to use Timer[^]
Examples:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/43daf8b2-67ad-4938-98f7-cae3eaa5e63f/[^]
http://www.dotnetperls.com/timer[^]
http://forum.codecall.net/topic/65434-c-working-with-timers/[^]
Microsecond and Millisecond C# Timer[^]
All about .NET Timers - A Comparison[^]
High-Performance Timer in C#[^]
小代码片段ab计时器。希望有所帮助。
Little code snippet about timer. Hope that help.
Timer timer;
int secondsToPlay;
private void StartPlay_Click(object sender, EventArgs e)
{
timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 1000;
DateTime Start = dateTimePicker1.Value;
DateTime End = dateTimePicker2.Value;
TimeSpan total = End - Start;
secondsToPlay = (int)total.TotalSeconds;
// Start video etc ...
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
secondsToPlay--;
if (secondsToPlay <= 0)
{
// Stop video etc ...
timer.Stop();
}
}
这篇关于如何检查DateTime是否已初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文