简体   繁体   English

从不是在C#中创建的线程的线程访问控件

[英]Control accessed from a thread other than the thread it was created on in C#

So I have a button that lately will be capable of open a door through SerialPort . 所以我最近有一个按钮,可以通过SerialPort打开一扇门。 To do that I will send something to it and wait for a response from it which is already configured and developed. 为此,我将向其发送消息并等待已经配置和开发的响应。

But I am getting some trouble to set a Thread.Sleep() . 但是我在设置Thread.Sleep()时遇到了麻烦。

This is what I got inside of the button: 这是我在按钮内部得到的:

private void cmdOpenDoor_Click(object sender, EventArgs e)
{
    Thread.Sleep(5000);
    Task.Factory.StartNew(this.Demo);
}

And the Demo method is: Demo方法是:

private void Demo()
{
    string questionMark= "?";
    string incoming = comport.ReadExisting();
    string carriageReturn = "\r";

    comport.Write(questionMark+ "(" + carriageReturn);

    Cursor.Current = Cursors.WaitCursor;

    if (txtTest.Text == "@q")
    {
        MessageBox.Show("Door opened!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        pbGreen.Show();
        pbRed.Hide();
     }
     else
     {
        MessageBox.Show("The door is already opened!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
}

When I will open the door it shows the "Door opened" message and when it tries to execute the pbGreen.Show() the applications stops and an error comes: 当我打开门时,它会显示“ Door open”(门已打开)消息,并在尝试执行pbGreen.Show() ,应用程序停止并出现错误:

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on 跨线程操作无效:从创建该线程的线程以外的线程访问控件

Do you have any idea how can I solve it? 你知道我该怎么解决吗? I cannot Invoke a PictureBox 我无法调用PictureBox

As the control you're trying to access (in this instance pbGreen and most likely pbRed as well) were created on the Main Thread, and you're trying to update them on a differnet thread, you get this error. 由于您要访问的控件(在本例中为pbGreen和最有可能的pbRed)是在主线程上创建的,而您试图在differentnet线程上进行更新时,会收到此错误。 To fix the issue, you need to make your calls thread-safe by invoking. 要解决此问题,您需要通过调用使调用成为线程安全的。

private void Demo()
{
    if (InvokeRequired) {
        Invoke(new MethodInvoker(() => { Demo(); }));
        return;
    }

    //Do stuff
}

However making your own method could be even better: 但是,制作自己的方法可能会更好:

public static void InvokeIfRequired(this ISynchronizeInvoke formControl, MethodInvoker action)
{
    if (formControl.InvokeRequired) {
        formControl.Invoke(action, new object[0]);
    } else {
        action();
    }
}

Then: 然后:

pbGreen.InvokeIfRequired(() => { pbGreen.Image = myImage; });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#控件'tabControl'从其创建线程以外的线程访问 - C# Control 'tabControl' accessed from a thread other than the thread it was created on 在不是在其上创建线程的线程上访问控件 - Control accessed on a thread other than the thread it was created on 从不是在其上创建线程的线程访问的控件“ x” - Control 'x' accessed from a thread other than the thread it was created on 从不是在其上创建的线程的线程访问控件“ webBrowser1” - Control 'webBrowser1' accessed from a thread other than the thread it was created on 从不是在其上创建线程的线程访问控件“ dataGridView1” - Control 'dataGridView1' accessed from a thread other than the thread it was created on 从不是在其上创建线程的线程访问控件“ ZedGraphControl” - Control 'ZedGraphControl' accessed from a thread other than the thread it was created on C#WPF线程从非 - C# WPF Thread accessed from a thread other than the ParallelFor | 跨线程操作无效:从创建该线程的线程以外的线程访问控件 - ParallelFor | Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on 跨线程操作无效:控制''从窗口形式创建的线程以外的线程访问 - Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on in window form 跨线程操作无效:从不是在其上创建线程的线程访问控件“ lbDatabase” - Cross-thread operation not valid: Control 'lbDatabase' accessed from a thread other than the thread it was created on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM