English 中文(简体)
我该如何检查下一场定时器火灾前还剩下多少时间?
原标题:How do I check how much time remains before timer fires next event?
  • 时间:2012-05-22 09:43:37
  •  标签:
  • c#

例如,我为 timer1.interval 设定了 5000 。 我想知道在计时器计时之前有多少间隔还剩下。 我该怎么做?

最佳回答

如何检查计时时间?

您无法。 定时器班无法提供任何方法检查定时器应点火前的剩余时间。 您所能做的就是跟踪计时器最后一次发射的时间, 并计算下个计时器前的剩余时间 。

问题回答
        DateTime MainTimer_last_Tick = DateTime.Now;
        System.Windows.Forms.Timer timer_Calc_Remaining;
        timer_Calc_Remaining.Enabled = true;
        timer_Calc_Remaining.Interval = 100;
        timer_Calc_Remaining.Tick += new System.EventHandler(this.timer_Calc_Remaining_Tick);

主计时器开始或选中时 :

   timer_Main.Enabled = true;
        MainTimer_last_Tick = DateTime.Now;


 private void timer_Calc_Remaining_Tick(object sender, EventArgs e)
        {
            int remaining_Milliseconds = (int)(MainTimer_last_Tick.AddMilliseconds(timer_Main.Interval).Subtract(DateTime.Now).TotalMilliseconds);
.../*
        int newValue = (timer_Main.Interval -remaining_Milliseconds) ;


        progressBar1.Maximum = timer_Main.Interval+1;

        progressBar1.Value = newValue ;*/



    }

您可以使用一个间隔为1000( 毫秒) 的替代计时器。 我不认为一米的间隔是一个真正的选项, 因为这样会使系统压力太大, 而且不可靠 。

每次这个计时器折叠时, 您可以检查秒数( 总是千位数吗? ), 然后从( 或使用 modulo) 5000 中减去 。

但我不能:

s 因为没有标志的少出现在 等同的标志之前 而不是相反的圆形

if (timer1.Interval <= 5000)
{
    //do something
}

您正在使用 Interval, 该属性将不会被更改( 总是 5000, 所以检查它是否小于 5000 是毫无意义的 ) 。 另外, 如果该代码在 < code>Timer1_ Tick < / code > 函数中, 它将不会有效 。 但是, 您需要的代码是 :

if (timer1.Interval <= 5000)
{
  //do something
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签