C# 如何设置整型变量以在数秒内重置
下面的方法为FindPederian()方法中标识的对象处理矩形。我已经指定了'rectCount'变量来显示每一帧上的矩形计数。而“maxCount”变量显示进程中计数的最大矩形数C# 如何设置整型变量以在数秒内重置,c#,emgucv,C#,Emgucv,下面的方法为FindPederian()方法中标识的对象处理矩形。我已经指定了'rectCount'变量来显示每一帧上的矩形计数。而“maxCount”变量显示进程中计数的最大矩形数 private Image<Bgr, Byte> imagingPedestrian(Image<Bgr, Byte> image) { System.Drawing.Rectangle[] results = pedestrianDetection.findPed
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;
}
private Image imagingPedestrian(图像图像)
{
System.Drawing.Rectangle[]results=pedestriandtection.findpedpedestrian(图像);
//System.Drawing.Rectangle[]results2=车辆检测.findVehicle(图像);
foreach(结果中的矩形rect)
{
CvInvoke.Rectangle(图像,rect,新的Bgr(颜色.红色).MCvScalar);
rectCount=results.Count();
label1.Text=rectCount.ToString();
如果(rectCount>maxCount1)
{
maxCount1=rectCount;
label8.Text=maxCount1.ToString();
}
其他的
{
label8.Text=maxCount1.ToString();
}
}
返回图像;
}
在这个问题中,我想得到的是每10秒的矩形计数。10秒后,maxCount变量应复位。然后,它应该在接下来的10秒内显示最大矩形计数。像wise一样,它应该在每10秒内迭代运行。无论如何都有办法实施它
提前感谢。在表单中添加一个。您可以将它从工具箱拖到表单上,或者如文档所示,您可以在表单中声明它。您可以将Interval
属性设置为10000(以毫秒为单位为10秒),并将Enabled
设置为true或调用Start()
方法
然后为
勾选事件添加一个处理程序。每次间隔结束时都会引发该事件。在该处理程序中,您可以执行更新。如果我理解正确,您需要使用类
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,以便在每次达到所需的值时重置,并且还将您的方法订阅到事件,以便每次计时器通过时,它都将执行所需的代码
只要将同样的方法应用到您的应用程序中,您的问题就会得到解决。“是否有实现它的方法”——当然有。这是编程。如果你能准确地说出你想要完成的事情,它是可以做到的。你已经试过什么了?如上所述,你的问题太宽泛了。请提供一个很好的例子来展示你所做的尝试,并对你遇到的具体问题进行精确、详细的解释。您可以使用秒表
来跟踪经过的时间,也可以使用.NET中的几个计时器
类中的一个来定期执行一些代码。我将指出,计算跟踪最大值(即过去10秒内的最大值)比每10秒重置一次最大值更有用/更常见。@PeterDuniho好吧,假设我有一个整数变量,它给它赋值了。我如何在10秒后将其重置为0,你是对的。我的链接断开了,但我指的是windows窗体计时器。我的回答最初表示,勾号
事件不会在UI线程上引发,但我错了(修复了它)。但是如果使用System.timer.Timers
类,则情况就是这样-已过
事件发生在不同的线程上。因此,在这种情况下,您需要确保对UI的任何更新都是在UI线程上完成的。@ScottHannen感谢您添加的说明!