public class A
{
   public int GetValue()
   {
      int val;
      ...
      val=111;//给返回结果赋值。
       ...
      for(int i=0;i<10000;i++)//执行时间需要很长的循环
      {
         ...
      }
      
      return val;
   }
}以上代码中,假设循环执行时间很长,为了提供效率,减少等待的时间,想创建一个子线程,让其去执行循环,而主线程在方法给val赋值时就返回结果,请问应该如何做呢?

解决方案 »

  1.   

    1.
    for(int   i=0;i <10000;i++)//执行时间需要很长的循环
                {
                  application.doevents();
                } 2
    Thread thread = new Thread(ThreadStart(delegate()
    {
      for(int   i=0;i <10000;i++)//执行时间需要很长的循环
                {
                      ...
                } 
    }));
    thread.start();
      

  2.   

    很简单啊,这样就可以了。
    using System.Threading;
    public   class   A 

          public   int   GetValue() 
          { 
                int   val; 
                ... 
                val=111;//给返回结果赋值。 
                  ... 
                Thread tempThread = new Thread(new ThreadStart(tmpThread));//定义线程
                tempThread.Start();//启动线程
                
                return   val; 
          } 
          public void tmpThread()
          {
              for(int   i=0;i <10000;i++)//执行时间需要很长的循环 
                { 
                      ... 
               } 
          }