多线程不起作用 [英] Multithreading doesn't work

查看:118
本文介绍了多线程不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我只是试图在两个列表框中并行打印1到100的数字.但是.​​..在第一个列表框之后,只有第二个列表框开始工作.我的编码有任何问题..还是多线程是这样的?请纠正我的错误在编码中.


I just trying to print the 1 to 100 numbers in two list boxes parallel.But...After the first listbox only the second list box starts working.Is my coding has any problem..or multithreading is like this?Please rectify my mistakes in the coding..

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

        }


谢谢

推荐答案

您正在做的不是多线程.
查看链接以了解更多信息
http://msdn.microsoft.com/en-us/library/aa645740 (v = vs.71).aspx [ .NET中的多线程 [ C#中的多线程概念 [
What you are doing is not multithreading.
Check out the links to learn more about it
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx[^]
Multithreading in .NET[^]
Multithreading Concepts in C#[^]


您的两个for循环是按顺序执行的. Thread.Sleep在添加两个项目之间仅等待10毫秒.这与多线程编程无关.为此,您必须产生两个单独的Thread并从那里进行添加.从非GUI线程访问GUI元素时,请确保使用Control.Invoke.

最好的问候,

-MRB
Your two for loops are executed sequentially. Thread.Sleep only waits 10 ms in between adding two items. This has nothing to do with multithreaded programming. For that you''d have to produce two separate Threads and do the adding from there. Please make sure to use Control.Invoke when accessing GUI elements from within a non GUI thread.

Best Regards,

—MRB


除了曼弗雷德(Manfred)的解决方案:请参阅我最近的解决方案,其中我将同时使用ControlDispatcher来解释调用:此编码正确吗? [
In addition to the solution by Manfred: please see my recent solution where I explain invocation using both Control and also Dispatcher: Is this correct coding?[^].

—SA


这篇关于多线程不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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