我有一个带有标签的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运行的时间.
我想你来自VB背景.
以下不是有效的C#代码.
lblStatus.Text = "timer running - Check inbox in " & common.aTimer.Interval - common.aTimer.Elapsed & " seconds!";
&
不用于c#中的字符串连接,请改用+
运算符.甚至更好.aTimer.Interval
是双重的,aTimer.Elapsed
是事件.您无法添加.这甚至没有任何意义.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.
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. 本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文