无法修复此错误 [英] Couldn't fix this error

查看:102
本文介绍了无法修复此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我应用以下代码时...

When i applied the following code...

private void LoadListbox1()
        {
            int i = 0;
            for (i = 0; i < 100; i++)
            {
                listBox1.Items.Add(i);
                Thread.Sleep(10);
            }
        }
        private void LoadListbox2()
        {
            int j = 0;
            for (j = 0;j < 100; j++)
            {
                listBox2.Items.Add(j);
                Thread.Sleep(10);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(LoadListbox1);
            Thread t2 = new Thread(LoadListbox2);
            t1.Start();
            t2.Start();
        }


我收到以下错误...您能告诉我这是什么吗?
跨线程操作无效:从创建该线程的线程以外的线程访问控件"listBox2"."


I got the following error...Could you tell me what is this?
''Cross-thread operation not valid: Control ''listBox2'' accessed from a thread other than the thread it was created on.''

推荐答案

请查看以下内容:如何:对Windows窗体控件进行线程安全调用 [ ^ ]

您需要的所有资源和信息都在这里.
关键是您需要在"InvokeRequired"时在控件上实现"Invoke"调用.
You need to check out this: How to: Make Thread-Safe Calls to Windows Forms Controls[^]

All the source and information you need is there.
The critical thing is that you need to implement the "Invoke" call on the controls when "InvokeRequired".


这篇关于无法修复此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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