简体   繁体   English

C#计时器的计算编译问题

[英]calculation compile issue with c# timer

I have a windows form with a label, and a system.net timer called aTimer . 我有一个带有标签的Windows窗体,以及一个名为aTimersystem.net计时器

The timer is created in an external class like this: 计时器是在外部类中创建的,如下所示:

// Create a timer with a 3 minute interval.
common.aTimer = new System.Timers.Timer(3000);

// Hook up the Elapsed event for the timer.
common.aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Set the Interval to 3 minutes (180000 milliseconds).
common.aTimer.Interval = 180000; // 3000; //  
common.aTimer.Enabled = true;
mTimerRunning = true;

and in my form I run the code OnTimedEvent to kick off a background worker 在我的表单中,我运行代码OnTimedEvent以启动后台工作人员

I want to show on my label the number of seconds before the next action is taken. 我想在标签上显示采取下一个动作之前的秒数。

I tried this but it won't compile: 我试过了,但无法编译:

lblStatus.BeginInvoke(new Action(() =>
                    {
        lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
                    }));

I have a compile error The event 'System.Timers.Timer.Elapsed' can only appear on the left hand side of += or -= 我有一个编译错误The event 'System.Timers.Timer.Elapsed' can only appear on the left hand side of += or -=

So what is the correct way to do this? 那么正确的方法是什么呢?

Ideally every tick of the timer I would update the label to show when the nxt run will happen. 理想情况下,计时器的每个滴答声都会更新标签以显示nxt运行的时间。

I guess you're from VB background. 我想你来自VB背景。

Following is not a valid c# code. 以下不是有效的C#代码。

lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
  • & is not used for string concatenation in c#, use + operator instead. &不用于c#中的字符串连接,请改用+运算符。 Or even better . 甚至更好
  • aTimer.Interval is double, aTimer.Elapsed is an event. aTimer.Interval是double, aTimer.Elapsed是一个事件。 You can't add it. 您无法添加。 It doesn't even makes sense. 这甚至没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM