C# 如何创建一个计时器事件,该事件将启动另一个计时器,以在引发时在变量中存储自以来经过的时间?
我有C# 如何创建一个计时器事件,该事件将启动另一个计时器,以在引发时在变量中存储自以来经过的时间?,c#,events,timer,event-handling,C#,Events,Timer,Event Handling,我有Machine和Production课程Machine类有一个名为IsProducing的属性,该属性指示机器中是否正在运行生产ProductionclassEstimatedTime用于计算生产剩余时间,而TimePassedBegin用于计算生产完成后经过的时间 所有检查返回true后,我开始生产,更改IsProducing=1,并计算我作为间隔传递给我创建的计时器的估计时间 我希望有一个事件,当计时器滴答作响时会引发该事件,IsProducing=0,当前时间被注册为EndTime,并
Machine
和Production
课程Machine
类有一个名为IsProducing
的属性,该属性指示机器中是否正在运行生产Production
classEstimatedTime
用于计算生产剩余时间,而TimePassedBegin
用于计算生产完成后经过的时间
所有检查返回true后,我开始生产,更改IsProducing=1编码>,并计算我作为间隔传递给我创建的计时器的估计时间
我希望有一个事件,当计时器滴答作响时会引发该事件,IsProducing=0
,当前时间被注册为EndTime
,并启动一个新计时器来计算TimePassedSince
我在Production.cs上有以下代码:
public delegate void EstimatedTimePassedHandler(Production production, Machine machine);
public event EstimatedTimePassedHandler ProductionStarted;
public event EstimatedTimePassedHandler ProductionCompleted;
我在Form.cs上有以下代码:
if (dh.StartProduction(serialNr, empName) != -1)
{
machine.IsProducing = 1;
Double secondsToGo = machine.CycleTime * production.NrOfLamallae;
TimeSpan time = TimeSpan.FromSeconds(secondsToGo);
DateTime dateTime = DateTime.Today.Add(time);
production.EstimatedTime = dateTime;
string displayTime = dateTime.ToString("hh:mm:ss");
tbResult.Text = $"The production is started and will be ready in {displayTime}.";
int milliseconds = (int)secondsToGo * 100;
Timer timer1 = new Timer { Interval = milliseconds };
timer1.Enabled = true;
timer1.Tick += new Production.EstimatedTimePassedHandler(this.EstimatedTimerTicked);
}
private void EstimatedTimerTicked(Production p, Machine m)
{
m.IsProducing = 0; //completed production, not producing anymore
Timer timer1 = new Timer();
/* here I assign the production.EndTime and production.TimePassedSince */
}
它给了我以下错误:
无法将类型“GUI\u Designs.Production.EstimatedTimePassedHandler”隐式转换为“System.EventHandler”
我怎样才能解决这个问题?用计时器滴答声处理此类事件的最佳实践是什么?如何在变量中存储时间?提前多谢 好的,如果只是关于处理程序:您对处理程序委托使用了错误的签名。它应该是:void EventHandler(object?sender,EventArgs e)
,因此在本例中,void EstimatedTimerChecked(object?sender,EventArgs e)
这对您没有多大用处。但我想知道:如果您的估计不正确或不精确怎么办?现实和软件会很快转移注意力。很快,工作机器将显示为“完成”,反之亦然…@Fildor我使用了此签名,但将其更改为当前签名,因为我需要能够操纵机器的生产和生产结束时间,以及启动第二个计时器来存储从那时起经过的时间。这个签名怎么可能?不是。使用System.Windows.Forms.Timer.Tick
Event,您将得到:sender和EventArgs。