替代在C#视频下载? [英] Alternative to Thread.Sleep in C#?
问题描述
我有一个运行时,它执行一系列按顺序行的code。 我想补充一个暂停之间。
I have a code which when run, it executes series of lines in sequence. I would like to add a pause in between.
目前,我有这样的
//do work
Thread.Sleep(10800000);
//do work
然而,这冻结了的软件,我想这是因为睡眠时间太长了。 我在网上搜索,我发现了另一个变通叫定时器。 可有人告诉我一个样品code定时器这工作就像视频下载?
This however freezes the software, and I think it's because sleep time is way too long. I searched online and I found another work around called Timer. Can someone show me a sample code for Timer which works just like Thread.sleep?
感谢你在前进。
编辑:我需要的软件等待3小时在进行code执行的其余部分之前。该软件旨在与机器沟通,我不能让在等待3小时的code中的其余部分执行。
Edit : I need the software to wait for 3 hours before proceeding with rest of the code execution. This software is meant to communicate with machines, and I cannot let the rest of the code execute while waiting for 3 hours.
推荐答案
您的软件将冻结,如果睡眠是在主线程。请记住该参数是毫秒。 10 800 000毫秒= 10800秒
Your software would freeze if that sleep is on the main thread. Keep in mind the parameter is in milliseconds. 10 800 000 milliseconds = 10 800 seconds
另一种方式来打发时间是通过一个时间间隔对象来代替。例如:
Another way to pass the time is to pass a TimeSpan object instead. Ex:
// Sleep for 10 seconds
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 10));
作为定时器:
As for timer:
您可以使用 System.Timers .Timer
;
You can use System.Timers.Timer
;
Timer timer = new Timer();
timer.Interval = 20; // milliseconds
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.AutoReset = true; // if not set you will need to call start after every event fired in the elapsed callback
timer.Start();
这篇关于替代在C#视频下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!