ASP.NET Tutorial/Development/Thread

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Sleep a server thread

<%@ Page %>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
  int i;
  System.Threading.Thread myThread;
  System.Threading.ThreadStart myThreadStart;
  myThreadStart=new System.Threading.ThreadStart(DoSomething);
  myThread=new System.Threading.Thread(myThreadStart);
  myThread.Start();
  for (i=1;i<6;i++)
  {
    System.Threading.Thread.Sleep(100);
    myLabel.Text+=i + "<BR>";
  }
  myThread.Abort();
}
private void DoSomething()
{
  int i;
  for (i=11;i<21;i++)
  {
    System.Threading.Thread.Sleep(100);
    myLabel.Text+=i + "<BR>";
  }
}
</script>
<HTML>
  <body>
    <form id="form1" method="post" runat="server">
    <asp:Label ID="myLabel" Runat="server"></asp:Label>
    </form>
  </body>
</HTML>


Start a thread

<%@ Page %>
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
  int i;
  System.Threading.Thread myThread;
  System.Threading.ThreadStart myThreadStart;
  myThreadStart=new System.Threading.ThreadStart(DoSomething);
  myThread=new System.Threading.Thread(myThreadStart);
  myThread.Start();
  for (i=1;i<11;i++)
  {
    System.Threading.Thread.Sleep(100);
    myLabel.Text+=i + "<BR>";
  }
  System.Threading.Thread.Sleep(100);
}
private void DoSomething()
{
  int i;
  for (i=11;i<21;i++)
  {
    System.Threading.Thread.Sleep(100);
    myLabel.Text+=i + "<BR>";
  }
}
</script>
<HTML>
  <body>
    <form id="form1" method="post" runat="server">
      <asp:Label ID="myLabel" Runat="server"></asp:Label>
    </form>
  </body>
</HTML>


TracingOutput.aspx

<%@Page trace="true" %>
<%@Import namespace="System.Threading" %>
<script language="C#" runat="server">
protected void Page_Load(object o, EventArgs e) {
  Trace.Write("MyInfo", "started Page_Load");
  for(int i = 0; i < 20; i++) {
    Thread.Sleep(100);
    Response.Write(i + "<br/>");
    Trace.Write("MyInfo", "loop iteration number " + i);
  }
  Trace.Write("MyInfo", "ending Page_Load");
}
</script>