Tag: 计时器

在Hibernate和Sleep之后,System.Timers.Timer如何在WPF应用程序中运行?

我在WPF应用程序中使用System.Timers.Timer 。 我想了解Timer在计算机hibernate和睡眠后的行为方式。 计算机从hibernate状态恢复后,我的应用程序出现了一些奇怪的问题。 我应该如何处理计时器,以及它们在计算机处于hibernate/睡眠模式时的行为方式? 我有一个午夜计时器,应该每个午夜工作以重置UI上的默认值。 以下是创建计时器的代码: private void ResetMidnightTimer() { // kill the old timer DisposeMidnightTimer(); _midnightTimer = new Timer(); // scheduling the timer to elapse 1 minute after midnight _midnightTimer.Interval = (DateTime.Today.AddDays(1).AddMinutes(1) – DateTime.Now).TotalMilliseconds; _midnightTimer.Elapsed += (_, __) => UpdateRecommendedCollectingTime(); _midnightTimer.Enabled = true; _midnightTimer.Start(); } 在UI页面的构造函数中,我调用调用ResestMidnightTimer()的方法并在事实上创建计时器。 之后,计时器只等了一晚。 当夜间(实际上是12:01 AM)到来时,计时器工作,按预期重置默认值,然后处理现有计时器。 最后它为第二天创建了一个新的午夜计时器。 但是如果我在那天尝试hibernate计算机,午夜计时器将无法工作,也不会重置默认值。 这是因为虽然冬眠它只是将事件处理推迟了相同的时间才被冬眠了吗?

调用线程必须是STA

我试图模拟一个抛射物并试图在定时事件中只创建一次标签,因为它在参数中有一个对象,我需要在定时事件中。 但是当我运行它时,我得到了这个线程错误。 请帮忙! 我的代码是: void onTimedEvent(Object source, ElapsedEventArgs e) { particle newProjectile; newProjectile = new particle(); bool LabelExist = false; if (LabelExist == false) { CreateLabels(newProjectile); } } aTimer = new System.Timers.Timer(200); aTimer.Elapsed += onTimedEvent; aTimer.Enabled = true; void CreateLabels(particle newProjectile) { DockPanel panelcontrol_displacement, panelcontrol_CurrentVel, panelcontrol_VerticalVel, panelcontrol_HorizontalVel, panelcontrol_Time; Label lbl_TimeUnits, lbl_TimeOutput, lbl_Time, lbl_DisplacementOutput, lbl_DisplacementUnits, lbl_Displacement, […]

计时器在c#中流逝时如何显示消息框

我将时间间隔设为100.当计时器结束时,显示消息框但我的屏幕充满了消息框。 如何通过一个指示计时器已过的消息框来停止它。 这是我的代码……你能指导我在哪里给停止计时器…… namespace timer1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { Timer t = new Timer(); t.Interval = 100; timer1.Enabled = true; timer1.Tick += new System.EventHandler(OnTimerEvent); } private void OnTimerEvent(object sender, EventArgs e) { MessageBox.Show(“time over”); } } }

C#Thread.Sleep(1000)在某些电脑上花费的时间超过1000毫秒

我写ac#program倒计时5秒。 我所做的是: new Task(() => { try { this.Invoke((Action)delegate() { label1.Text = “5”; // 4, 3, 2..etc }); } Thread.Sleep(1000); } catch { // form could be disposed break; } } }).Start(); 这可以在我的电脑上运行,但是,当我将程序复制到平板电脑时,Thread.Sleep(1000)给出的时间超过1秒。 换句话说,从5,4,3,2,1更新标签需要5秒以上(实际上超过10秒)。 或者,更新label1.Text =“5”等需要很长时间? 这对我没有意义。 我想知道出了什么问题?

将计时器添加到Xamarin应用程序(c#)

所以我需要一个计时器从60秒倒计时。 我是Xamarin的新手,不知道它接受了什么。 有关如何开始的任何建议? 它将用于android 你可以使用System.Timers.Timer吗?

如何准确地使用调度程序计时器来控制WPF中的帧速率?

当我尝试使用System.Windows.Threading.DispatcherTimer实例控制WPF中的帧速率时遇到问题。 为了尝试DispatcherTimer的有效性,我创建了一个简单的WPF Demo,其中包含一个带有文本框和按钮的窗口。 单击该按钮时,DispatcherTimer实例开始根据文本框中的双精度值作为间隔并且StopWatch同时启动,计数器变量在每个DispatcherTimer刻度上增加1。 当StopWatch.ElaspedMilliSeconds> 1000(超过1秒)时,计时器停止计时并且秒表也会重置,弹出一个消息框以显示计数器的值。 因此,如果输入33.3(1000/30),计数器值应该在30左右。 但结果是20左右。我请求帮助是否有人可以帮助检查下面的源代码中的问题。 提前致谢。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TimerDemo { public partial class MainWindow : Window { private System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer(); public MainWindow() […]

定时器开始时间和结束时间计算额外提供4秒

我正在使用WP8,我正在使用计时器,我正在计算总时间。 这就是我这样做的方式: timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(1);//interval for timer is 1 sec timer.Tick += new EventHandler(timer_Tick);//after the timer expires, this event is fired timer.Start(); startDateTime = DateTime.Now; DateTime et = DateTime.Now; Debug.WriteLine(“st is “+startDateTime+ ” et is “+et); TimeSpan myDateResult = et – startDateTime; double seconds = myDateResult.TotalSeconds; Debug.WriteLine(“difference is ” + seconds); […]

协同程序为何停止工作/执行

我有一个3秒倒数计时器,在游戏未启用时激活。 几天前我让它正常工作但现在它不再起作用了。 它被阻塞在数字3上。这是代码: IEnumerator Timer() { Time.timeScale = 0; objectWithGSScript.scoreText.fontSize = 300; objectWithGSScript.scoreText.text = “” + 3; yield return WaitOneSecond(); objectWithGSScript.scoreText.text = “” + 2; yield return WaitOneSecond(); objectWithGSScript.scoreText.text = “” + 1; yield return WaitOneSecond(); objectWithGSScript.scoreText.text = “Go!”; yield return WaitOneSecond(); Time.timeScale = 1f; objectWithGSScript.scoreText.text = objectWithGSScript.score.ToString(); } IEnumerator WaitOneSecond() { float start = […]

不准确的.NET计时器?

我正在开发一个应用程序,我需要从服务器获取当前日期(它与机器的日期不同)。 我从服务器收到日期并使用简单的Split我创建一个新的DateTime: globalVars.fec = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, int.Parse(infoHour[0]), int.Parse(infoHour[1]), int.Parse(infoHour[2])); globalVars是一个类,fec是一个公共静态变量,因此我可以在应用程序的任何地方访问它(我知道编码错误…)。 现在我需要有一个计时器来检查该日期是否等于我在List中存储的某些日期,如果它相等,我只需要调用一个函数。 List fechas = new List(); 在必须从服务器获取日期之前我使用计算机的日期,所以要检查日期是否匹配我使用的是: private void timerDatesMatch_Tick(object sender, EventArgs e) { DateTime tick = DateTime.Now; foreach (DateTime dt in fechas) { if (dt == tick) { //blahblah } } } 现在我有来自服务器的日期,所以DateTime.Now不能在这里使用。 相反,我已经创建了一个Interval = 1000的新计时器,并且在tick上我使用以下内容向globalVars.fec添加1秒: globalVars.fec = globalVars.fec.AddSeconds(1); 但时钟不准确,每30分钟时钟损失约30秒。 有没有其他方法可以做我想做的事情? 我考虑过使用threading.timer,但我需要访问其他线程和非静态函数。

我可以使用计时器每隔x毫秒更新一次标签

这是我的代码: Stopwatch timer = new Stopwatch(); timer.Start(); while (timer.ElapsedMilliseconds < 3000) { label1.Text = Convert.ToString( timer.ElapsedMilliseconds ); } timer.Stop(); 我的意思是实时更新标签的文本,所以如果timer.ElapsedMilliseconds == 1350 ,那么label1.Text = 1350 。 我怎样才能做到这一点? 提前致谢!