I would suggest you use a separate thread to do the job

解决方案 »

  1.   

    ASP.net创建线程可否也有该类问题,如何创建,各位高手快伸手相救!!!
      

  2.   

    see
    Threading in .NET Part 1
    http://www.dotnetjunkies.com/tutorials.aspx?tutorialid=296code:using System; 
    using System.Threading; public class my3Methods {   public void myMethod1() { 
        Console.WriteLine("...myThread1 created!"); 
        Thread.Sleep(5000); 
        Console.WriteLine("...myThread1 has finished!"); 
      }   public void myMethod2() { 
        Console.WriteLine("...myThread2 created!"); 
        Thread.Sleep(3000); 
        Console.WriteLine("...myThread2 has finished!"); 
      }   public void myMethod3() { 
        Console.WriteLine("...myThread3 created!"); 
        Thread.Sleep(1000); 
        Console.WriteLine("...myThread3 has finished!"); 
      } 
    } public class ThreadTest {   public static int Main(String[] args) {     my3Methods testClass = new my3Methods();     Thread myThread1 = new Thread(new ThreadStart(testClass.myMethod1)); 
        myThread1.Start();     Thread myThread2 = new Thread(new ThreadStart(testClass.myMethod2)); 
        myThread2.Start();     Thread myThread3 = new Thread(new ThreadStart(testClass.myMethod3)); 
        myThread3.Start();     return 0; 
      } 
    }
      

  3.   

    先谢了,可我想知道在Web中调用是否还会出现上述情况,因为文件较大,而页面必须等待显示文件处理完的结果信息.盼回答!
      

  4.   

    怎么没了声音了,我试了在Web中调用线程,现在是页面错误没了,可线程到几分钟后还是down了,因为仅插入不到两万条记录!盼高手回答!!!
      

  5.   

    you need to start the thread first, you probably need to pass the Session into the thread, when the work is done, you will set some session variable, or some field in a database, and your main page will refresh periodically and check the session variable or the value in the database until it is done
      

  6.   

    here is an example:TestCalc.cs: (compile it into TestCalc.dll and put it in the bin subdirectory)
    using System;
    using System.Threading;public class TestCalc
    {
      System.Web.SessionState.HttpSessionState m_oSession;
      public TestCalc(System.Web.SessionState.HttpSessionState s)
      {
    m_oSession = s;
      }  public void Calc()
      {
         long nCount = 0;
         for (int i=0; i < 10000; i++)
         {
    Thread.Sleep(5);
    nCount++;
         }     m_oSession["done"] = true;
      }}
    CalTest.aspx:<%@ Assembly Name="TestCalc" %>
    <%@ Import Namespace="System.Threading" %>
    <script language="C#" runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
      DisplayInfo();
    }void RunCal(Object sender, EventArgs e)
    {
    Session["done"] = false;
    Session["startTime"] = DateTime.Now; TestCalc c = new TestCalc(Session);
    Thread myThread = new Thread(new ThreadStart(c.Calc)); 
    myThread.Start();  DisplayInfo();
    }void DisplayInfo()
    {
      if (Session["done"] != null)
      {
    if ((bool)Session["done"])
    {
    TimeSpan ts = DateTime.Now - (DateTime)Session["startTime"];
    label1.Text = "done! total time: " + ts.TotalSeconds.ToString("F2") + " seconds";
    }
    else
    {
    btn.Visible = false;   TimeSpan ts = DateTime.Now - (DateTime)Session["startTime"];
    label1.Text = "still calculating...have been waiting for " + ts.TotalSeconds.ToString("F2") + " seconds"; Response.AddHeader("REFRESH","5");
    }
      }
    }
    </script>
    <form runat="server">
    <asp:button id="btn" text="run" runat="server" onclick="RunCal"/>
    <asp:label id="label1" runat="server" />
    </form>
      

  7.   

    该问题已解决,谢谢!特别是saucer!以后有问题再请教.
      

  8.   

    我是新来的,不知如何给结贴,请指明?? 
      还有问题问saucer高手,同一问题,Response.AddHeader("REFRESH","5")是每隔五秒刷一次,可是也将我原网页上的配置给全刷新了,如:有两个下拉控件选中的内容全给刷新为初始值了,如何控制??在线等候!!
      

  9.   

    then do not use
    Response.AddHeader("REFRESH","5");you might want to add a client side script, like
    <script language='javascript'>function window.onload(){setTimeout('form1.submit()',5000);}</script>
      

  10.   

    能用C#写吗? 我对script语句不熟,不知写在哪,要不麻烦你写细些,谢谢!!
      

  11.   

    Hello,saucer! 别逗我了,我可是满怀希望在盼望您了!
      

  12.   

    Response.AddHeader("REFRESH","5");===>(change "form1" to the form id in your page)this.RegisterStartupScript("Startup", "<script language='javascript'>function window.onload(){setTimeout('form1.submit()',5000);}</" + "script>");
      

  13.   

    >>>可也无法监控线程运行的情况
    use a session variable, inside the other thread, periodically update its valuein your page, check this session variable's value>>>5000是超时的秒吗
    yes
      

  14.   

    我设是想在线程中根据处理的结果不同,Session["ResultText"]的值也不同,在我的页面上每隔5秒监控该Session的值,若还为初值,则提示用户"正在处理...",否则将运行结果显示出来.可是遇到以下三个问题:
    1:线程中的Session变化了,无法在Page中及时反映出来。
    2:如何编制循环监控程序(C#)。
    3:Page 不刷新,可能有会出现我最初问的页面出错的老问题,特别是针对大文件!
    切盼帮助!
      

  15.   

    you need to refresh the page periodically!
      

  16.   

    该问题已解决,谢谢saucer,可不只如何给分??  结贴管理委员会的同志们给个提示.