Click here to Skip to main content
15,888,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
}
Posted
Updated 21-Sep-16 21:36pm

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900