跨线程操作无效:控制“从创建它的线程以外的线程访问 [英] cross-thread operation not valid: Control " accessed from a thread other than the thread it was created
本文介绍了跨线程操作无效:控制“从创建它的线程以外的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每一个,
i有一种方法,我想在面板控件中显示图像。
我先放这个图片在图片框中,然后我将它添加到面板控件。
i我使用两个线程,但它给我错误,因为我在这个问题的提交中提到。
错误我上线
this.agentbox.left = px;
请有人给我建议。
private 委托 void DrawOnPanelCircleDelegate(Point pp,Bitmap r);
public void DrawOnPanelCircle(Point p,Bitmap ratImage)
{
if (InvokeRequired)
{
DrawOnPanelCircleDelegate drwOnPanelCircle = new DrawOnPanelCircleDelegate((pp,r)= > DrawOnPanelCircle(p,ratImage));
this .BeginInvoke(drwOnPanelCircle, new object [] {p,ratImage});
}
lock (_locker)
{
this .agentBox .Image = ratImage;
this .agentBox.SizeMode = PictureBoxSizeMode.StretchImage;
// this.Invoke((MethodInvoker)委托
// {
此 .agentBox.Left = pX;
this .agentBox.Top = pY;
// });
// Application.DoEvents();
}
解决方案
我不知道你发布了一个单独的问题(这是正确的事情,并刚刚回答:如何在图像上绘制多个图像并通过图片框显示 [ ^ ]。
-SA
尝试
调用事件而不用担心跨线程异常 [ ^ ]
http://msdn.microsoft.com/en-us/library/ms171728%28v=vs.85% 29.aspx [ ^ ]
hi every one,
i have a method in which i want to display an image in the panel control.
first i put this picture in the picturebox then i add it to the panel control.
i am using two threads but it give me the error as i have mentioned in the subjection of this question.
error i am getting at line
this.agentbox.left = p.x;
anybody could give me suggestion please.
private delegate void DrawOnPanelCircleDelegate(Point pp, Bitmap r );
public void DrawOnPanelCircle(Point p, Bitmap ratImage)
{
if (InvokeRequired)
{
DrawOnPanelCircleDelegate drwOnPanelCircle = new DrawOnPanelCircleDelegate((pp, r) => DrawOnPanelCircle(p, ratImage));
this.BeginInvoke(drwOnPanelCircle, new object[] { p, ratImage });
}
lock (_locker)
{
this.agentBox .Image = ratImage;
this.agentBox.SizeMode = PictureBoxSizeMode.StretchImage;
//this.Invoke((MethodInvoker)delegate
//{
this.agentBox.Left = p.X;
this.agentBox.Top = p.Y;
//});
// Application.DoEvents();
}
解决方案
I did not know that you posted a separate question (which was right thing to do) and just answered: how to draw more than one images on an image and to display through picturebox[^].
—SA
Try
Invoking events without worrying about cross-thread exceptions[^]
http://msdn.microsoft.com/en-us/library/ms171728%28v=vs.85%29.aspx[^]
这篇关于跨线程操作无效:控制“从创建它的线程以外的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文