有一段循环代码,执行一段时间后就会出现无响应。我想在循环中加入线程,即每循环一次建立一个新的线程,每循环完成一次关闭这个线程。代码如下,应该如何实现? while (persents <= 100)
            {
                
                 
                button5_Click(sender, e);
                Applications.DoEvents();
                Thread.Sleep(500);
               button2_Click(sender, e);
               Applications.DoEvents();
               Thread.Sleep(500);
               int i = int.Parse(npcid.Text);
                i = i + 1;
                npcid.Text = i.ToString();
                Applications.DoEvents();
                Thread.Sleep(500);
                nownamelong = nownamelong + 1;
                string nowsst = nownamelong.ToString();
                label2.Content = "当前正在对第" + nowsst + "个数据进行操作!";                Applications.DoEvents();
                Thread.Sleep(500);
               per = (double)nownamelong / wherenamelong;
                persents = (double)per * 100;
                percon.Text = persents + "%";
                progressBar1.Value = persents;
                Applications.DoEvents();
                Thread.Sleep(500);
            }

解决方案 »

  1.   


                int i = 100;
                while (i > 0)
                {
                    System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(YouFun));
                    th.Start();
                    i--;
                    //在合适的地方结束线程,或者让线程自己结束
                    th.Abort();
                }
      

  2.   

    backgroundWorker 结合progressbar
    http://topic.csdn.net/u/20091203/11/a1371284-07c3-4ed2-bb5e-4f522c57189f.html
     while (i > 0) 

    Thread thread = new Thread(new ParameterizedThreadStart(SomeMethod));}或
    var a = new Thread(A);
    a.Start();
    static void A()
    {
    }
      

  3.   

    backgroundWorker 结合progressbar
    http://topic.csdn.net/u/20091203/11/a1371284-07c3-4ed2-bb5e-4f522c57189f.html
     while (i > 0)  
    {  
    Thread thread = new Thread(new ParameterizedThreadStart(SomeMethod));}或
    var a = new Thread(A);
    a.Start();
    static void A()
    {
    }