[英]Cross-thread operation not valid using timer and 2 buttons
i got this problem...我遇到了这个问题...
What would be a good solution for this?对此有什么好的解决方案?
private void button1_Click(object sender, EventArgs e)
{
aTimer.Enabled = true;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
aTimer.Enabled = false;
}
private void timer_is_working(object source, ElapsedEventArgs e)
{
aTimer.Enabled = false;
button1.Enabled = true;
}
Thanks!谢谢! Kind regards Daniel Ruescher亲切的问候丹尼尔·鲁舍尔
So you did not make it clear, but based on the ElapsedEventArgs
type it seems that timer_is_working
is the Elapsed
event of a System.Timers.Timer
instance.所以你没有说清楚,但基于ElapsedEventArgs
类型, timer_is_working
似乎是System.Timers.Timer
实例的Elapsed
事件。
Be aware that the .NET Framework Class Library includes four classes named Timer, each of which offers different functionality:请注意,.NET Framework 类库包括四个名为 Timer 的类,每个类提供不同的功能:
- System.Timers.Timer: fires an event at regular intervals. System.Timers.Timer:定期触发事件。 The class is intended for use as a server-based or service component in a multithreaded environment.该类旨在用作多线程环境中的基于服务器或服务的组件。
- System.Threading.Timer: executes a single callback method on a thread pool thread at regular intervals. System.Threading.Timer:定期在线程池线程上执行单个回调方法。 The callback method is defined when the timer is instantiated and cannot be changed.回调方法是在定时器实例化时定义的,不能更改。 Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment.与 System.Timers.Timer 类一样,此类旨在用作多线程环境中的基于服务器或服务组件。
- System.Windows.Forms.Timer: a Windows Forms component that fires an event at regular intervals. System.Windows.Forms.Timer:一个 Windows 窗体组件,它定期触发一个事件。 The component is designed for use in a single-threaded environment.该组件专为在单线程环境中使用而设计。
- System.Web.UI.Timer: an ASP.NET component that performs asynchronous or synchronous web page postbacks at a regular interval. System.Web.UI.Timer:一个 ASP.NET 组件,它定期执行异步或同步网页回发。
If this is a Windows.Forms app, use a System.Windows.Forms.Timer
instead (you find it in Toolbox/Components).如果这是 Windows.Forms 应用程序,请改用System.Windows.Forms.Timer
(您可以在工具箱/组件中找到它)。 Its Tick
event is raised in the UI thread so can access your controls from there.它的Tick
事件在 UI 线程中引发,因此可以从那里访问您的控件。
If you have a special reason to use the System.Timers.Timer
(eg. precision), you must wrap your access into an Invoke
call:如果您有使用System.Timers.Timer
的特殊原因(例如精度),您必须将您的访问权限包装到一个Invoke
调用中:
Invoke(new Action(() => { button1.Enabled = true; }));
You can use Invoke
to update UI by using UI thread.您可以使用Invoke
通过使用 UI 线程来更新 UI。
Try this example试试这个例子
private void timer_is_working(object source, ElapsedEventArgs e)
{
ExecuteSecure(() => aTimer.Enabled = false);
ExecuteSecure(() => button1.Enabled = true);
}
private void ExecuteSecure(Action action)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(() =>
{
action();
}));
}
else
{
action();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.