【发布时间】:2013-10-26 17:29:11
【问题描述】:
我正在尝试使用计时器创建每 500 毫秒重绘一次的图形,但我一直遇到跨线程操作。有人可以告诉我为什么会这样吗?
错误:
Cross-thread operation not valid: Control 'GraphicsBox' accessed from a thread other than the thread it was created on.
我正在使用 WinForms,并且在主窗体中有一个名为“GraphicsBox”的 PictureBox:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace NamespaceName
{
public partial class FormName : Form
{
Graphics g;
public FormName()
{
InitializeComponent();
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 500;
t.Enabled = true;
t.Elapsed += (s, e) => this.GraphicsBox.Invalidate(true);
}
private void FormName_Load(object sender, EventArgs e)
{
this.GraphicsBox.Paint += new PaintEventHandler(OnPaint);
}
protected void OnPaint(object sender, PaintEventArgs e)
{
g = e.Graphics;
//Draw things
}
}
}
有什么方法可以从计时器的“滴答声”(或“已过”)触发OnPaint 事件?我相信这会成功。我要做的就是重绘图形对象,我将更改代码中的内容以使其以不同的方式绘制。
【问题讨论】:
-
为什么不直接使用 system.windows.forms.timer?这将自动使用正确的线程...
-
@MarcGravell 叹了口气,我知道它必须是简单的...谢谢!
-
这段代码其实是有效的,从线程池线程调用Invalidate()就可以了。