代码执行正确,但输出未到 [英] The code is executing correctly,but output not coming

查看:78
本文介绍了代码执行正确,但输出未到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我将以下代码用于多线程...这是正确的吗?

Hi,
I applied the following code for multithreading...Is this correct?

protected void Button1_Click(object sender, EventArgs e)
   {
       try
       {
           Thread tid1 = new Thread(new ThreadStart(this.Thread1));
           Thread tid2 = new Thread(new ThreadStart(this.Thread2));

           tid1.Start();
           tid2.Start();
       }
       catch
       {
       }
   }
   public void Thread1()
   {
       try
       {
           for (int i = 0; i < 100; i++)
           {
               ListBox1.Items.Add(i.ToString());
               Thread.Sleep(10);
           }
       }
       catch (Exception ex)
       {
           Label1.Text = ex.Message;
       }
   }
   private void Thread2()
       {
           try
           {
               for (int i = 0; i < 100; i++)
               {
                   ListBox2.Items.Add(i.ToString());
                   Thread.Sleep(50);

               }
           }
           catch
           {
           }
       }


谢谢

推荐答案

抱歉,不正确.

1)您的Web应用程序不等待两个线程完成,因此响应不包括添加到线程中的数据.
2)永远不要使用Thread.Sleep,总会有更好的方法,它不会使用那种丑陋的东西.
3)切勿使用将捕获块留空.
Sorry, NO it is not correct.

1) Your web application doesn''t wait on the two threads to finish, thus the reponse does not include the data added in the threads.
2) Never use Thread.Sleep there is always a better way, which doesn''t use that ugly thing.
3) Never use leave a catch-block empty.


这篇关于代码执行正确,但输出未到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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