如何将整数变量设置为在数秒内重置

本文关键字:整数 变量 设置 | 更新日期: 2023-09-27 17:54:03

下面的方法处理findPedestrian()方法中标识的对象的矩形。我已经分配了"rectCount"变量来显示每帧上的矩形计数。而'maxCount'变量显示了在处理过程中计数的最大矩形数。

private Image<Bgr, Byte> imagingPedestrian(Image<Bgr, Byte> image)
    { 
        System.Drawing.Rectangle[] results = pedestrianDetection.findPedestrian(image);
        //System.Drawing.Rectangle[] results2 = vehicleDetection.findVehicle(image);
        foreach (Rectangle rect in results)
        {
            CvInvoke.Rectangle(image, rect, new Bgr(Color.Red).MCvScalar);
            rectCount = results.Count();
            label1.Text = rectCount.ToString();
            if(rectCount > maxCount1)
            {
                maxCount1 = rectCount;
                label8.Text = maxCount1.ToString();
            }
            else
            {
                label8.Text = maxCount1.ToString();
            }
        }
        return image;
    }

在这个问题中,我想要得到的是每10秒得到一个矩形的计数。10秒后,maxCount变量应该被重置。然后,它应该在接下来的10秒内显示最大矩形数。同样,它应该在每10秒内迭代运行一次。有办法实现它吗?

Thanks in Advance.

如何将整数变量设置为在数秒内重置

如果我理解正确的话,您需要使用System。计时器

来自MSDN的示例:

using System;
using System.Timers;
public class Example
{
   private static System.Timers.Timer aTimer;
   public static void Main()
   {
      SetTimer();
      Console.WriteLine("'nPress the Enter key to exit the application...'n");
      Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
      Console.ReadLine();
      aTimer.Stop();
      aTimer.Dispose();
      Console.WriteLine("Terminating the application...");
   }
   private static void SetTimer()
   {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(2000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.AutoReset = true;
        aTimer.Enabled = true;
    }
    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                          e.SignalTime);
    }
}
// The example displays output like the following:
//       Press the Enter key to exit the application...
//
//       The application started at 09:40:29.068
//       The Elapsed event was raised at 09:40:31.084
//       The Elapsed event was raised at 09:40:33.100
//       The Elapsed event was raised at 09:40:35.100
//       The Elapsed event was raised at 09:40:37.116
//       The Elapsed event was raised at 09:40:39.116
//       The Elapsed event was raised at 09:40:41.117
//       The Elapsed event was raised at 09:40:43.132
//       The Elapsed event was raised at 09:40:45.133
//       The Elapsed event was raised at 09:40:47.148
//
//       Terminating the application...

正如你所看到的,你创建了一个计时器,将AutoReset设置为true,这样每次达到所需的值时它都会重置,并且还订阅了你的方法event,这样每次计时器通过时它都会执行所需的代码。

只要把同样的方法应用到你的应用程序中,你的问题就会解决。

在表单中添加计时器。您可以将它从工具箱拖到表单上,或者如文档所示,您可以直接在表单中声明它。您可以将Interval属性设置为10000(即以毫秒为单位的10秒)并将Enabled设置为true或调用Start()方法。

Tick事件添加处理程序。每次间隔结束时都会引发该事件。在这个处理程序中,您可以执行更新。