跨线程操作无效:控制“面板”从其创建的线程以外的线程访问 [英] Cross-thread operation not valid: control 'panel' accessed from a thread other than the thread it was created on

查看:77
本文介绍了跨线程操作无效:控制“面板”从其创建的线程以外的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试按下按钮并发生此类错误。短按工作更好但按下长按(跨线程操作无效:控制'面板'从其创建的线程以外的线程访问。)发生了这种类型的错误。





我尝试过:



loopTimer = new Timer();

loopTimer.Interval = 1000;

loopTimer.Enabled = false;

loopTimer.Elapsed + = loopTimerEvent;

loopTimer .AutoReset = true;



public bool long_press = false;

private void loopTimerEvent(object source,ElapsedEventArgs e)

{



loopTimer.Dispose();

long_press = true;

panel_animatedOrders.Show();





}

public void newdowntable_Click (对象发送者,MouseEventArgs e)

{

loopTimer.Enabled = true;

label1.Text =((按钮)发送者).Name ;

btn_one.Text =((按钮)发件人).Name ++A;

btn_two.Text =((按钮)发件人).Name + +B;

btn_three.Text =((按钮)发送者).Name ++C;

btn_four.Text =((按钮)发送者).Name ++D;



k_one.Text =((按钮)发送者).Name +。1;

k_two.Text =((Button)sender).Name +。2;

k_three.Text =((Button)sender).Name +。3;

k_four.Text =((按钮)发件人).Name +。4;



btn_kitchenOrderNumber.Text =((按钮)发件人).Name;

}

public void newupTable_Click(object sender,MouseEventA rgs e)

{

loopTimer.Enabled = false;

if(long_press == false)

{

label1.Text =((Button)sender).Name;

Control ctls = this.Parent;

KOTOrder vieret = new KOTOrder( label1.Text,btn_kitchenOrderNumber.Text);

ctls.Controls.Clear();

ctls.Controls.Add(vieret);

}

long_press = false;

}

解决方案

错误信息非常明确:

从创建它的线程以外的线程访问控制'面板'。



您只能访问来自原始UI线程:如果您尝试从任何其他线程访问它们,您将收到错误。这适用于所有控件的所有属性,方法和字段,包括它们所属的表单。



要从不同的线程访问控件,您必须调用它:如何:从线程处理控件 [ ^ ]或使用BackgroundWorker,它可以通过事件将进度报告回主线程。


I try to press button hold and this type of error occurred. Short Press work better but while press long (Cross-thread operation not valid: Control 'panel' accessed from a thread other than the thread it was created on.) this type of error occurred.



What I have tried:

loopTimer = new Timer();
loopTimer.Interval = 1000;
loopTimer.Enabled = false;
loopTimer.Elapsed += loopTimerEvent;
loopTimer.AutoReset = true;

public bool long_press = false;
private void loopTimerEvent(object source, ElapsedEventArgs e)
{

loopTimer.Dispose();
long_press = true;
panel_animatedOrders.Show();


}
public void newdowntable_Click(object sender, MouseEventArgs e)
{
loopTimer.Enabled = true;
label1.Text = ((Button)sender).Name;
btn_one.Text = ((Button)sender).Name + " " + "A";
btn_two.Text = ((Button)sender).Name + " " + "B";
btn_three.Text = ((Button)sender).Name + " " + "C";
btn_four.Text = ((Button)sender).Name + " " + "D";

k_one.Text = ((Button)sender).Name + ".1";
k_two.Text = ((Button)sender).Name + ".2";
k_three.Text = ((Button)sender).Name + ".3";
k_four.Text = ((Button)sender).Name + ".4";

btn_kitchenOrderNumber.Text = ((Button)sender).Name;
}
public void newupTable_Click(object sender, MouseEventArgs e)
{
loopTimer.Enabled = false;
if (long_press==false)
{
label1.Text = ((Button)sender).Name;
Control ctls = this.Parent;
KOTOrder vieret = new KOTOrder(label1.Text, btn_kitchenOrderNumber.Text);
ctls.Controls.Clear();
ctls.Controls.Add(vieret);
}
long_press = false;
}

解决方案

The error message is pretty explicit:

Control 'panel' accessed from a thread other than the thread it was created on.


You can only access controls from the original UI thread: if you try to access them from any other you will get an error. That applies to all properties, methods, and fields of all controls, including the form they are part of.

To access a control from a different thread you have to invoke it: How to: Manipulate Controls from Threads[^] or use a BackgroundWorker which can report progress back to the main thread via an event.


这篇关于跨线程操作无效:控制“面板”从其创建的线程以外的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆