控件''从创建它的线程以外的线程访问。 [英] Control '' accessed from a thread other than the thread it was created on.

查看:67
本文介绍了控件''从创建它的线程以外的线程访问。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Win App中收到错误。



跨线程操作无效:从创建线程以外的线程访问控件'' on。



这是我的代码



I am Getting an error in My Win App.

Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

Here Is My Code

private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
login();
}

private void dc(object sender)
        {
            if (base.InvokeRequired)
            {
                base.BeginInvoke(new ObjectHandler(dc), new object[] { sender });
            }
            else
            {

                textBox1.BackColor = Color.Yellow;
            }
            if (checkBox5.Checked == true)
            {
loginToolStripMenuItem.PerformClick();
 }
        }







错误在线:loginToolStripMenuItem。 PerformClick();




The Error is raising in line : loginToolStripMenuItem.PerformClick();

推荐答案

你需要在else括号内拥有访问控件的所有代码。



base 更改为。我错过了那个小细节。



You need to have all code that access a control inside the else brackets.

Change base to this. I missed that little detail.

private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
    login();
}

private void dc(object sender)
{
    if (this.InvokeRequired)
    {
        this.BeginInvoke(new ObjectHandler(dc), new object[] { sender });
    }
    else
    {

        textBox1.BackColor = Color.Yellow;
        if (checkBox5.Checked == true)
        {
            loginToolStripMenuItem.PerformClick();
        }
    }
}


这篇关于控件''从创建它的线程以外的线程访问。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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