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

calculation compile issue with c# timer
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我有一个带有标签的Windows窗体,以及一个名为的system.net计时器aTimer.

计时器是在外部类中创建的,如下所示:

// 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;

在我的表单中,我运行代码OnTimedEvent以启动后台工作人员

我想在标签上显示采取下一个动作之前的秒数.

我试过了,但无法编译:

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

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

那么正确的方法是什么呢?

理想情况下,计时器的每个滴答声我都会更新标签以显示nxt运行的时间.

速聊1:
解决过程1

我想你来自VB背景.

以下不是有效的C#代码.

lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
  • &不用于c#中的字符串连接,请改用+运算符.甚至更好.
  • aTimer.Interval是双重的,aTimer.Elapsed是事件.您无法添加.这甚至没有任何意义.
速聊1:
+1是,如果出现问题,我将"+"更改为"&".有没有一种方法可以获取点数-我想这就是我想要的.

I have a windows form with a label, and a system.net timer called aTimer.

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

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 -=

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.

Solutions1

I guess you're from VB background.

Following is not a valid c# code.

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. Or even better.
  • aTimer.Interval is double, aTimer.Elapsed is an event. You can't add it. It doesn't even makes sense.
Talk1:
+1 yeah I changed the '+' to a '&' in case that was the issue. Is there a way to get the count of ticks - I guess that's what I want.
转载于:https://stackoverflow.com/questions/23712054/calculation-compile-issue-with-c-sharp-timer

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入