C of asp. net Multithreaded Usage Example of can be used to process multiple tasks at the same time

  • 2021-10-16 02:31:17
  • OfStack

This article illustrates the multithreading usage of C # (asp. net). Share it for your reference, as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Threading;
using System.Web.UI.WebControls;
public partial class muti_thread : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    Thread Thread1 = new Thread(new ThreadStart(CalcSum));
    Thread Thread2 = new Thread(new ThreadStart(CalcGap));
    Thread1.Start();
    Thread2.Start();
    Thread1.Join();
    Thread2.Join();
  }
  // Summation method 
  protected void CalcSum()
  {
    long sum = 0;
    for (long i = 0; i < 100; i++)
    {
      sum += i;
      Response.Write(string.Format("Thread1-->i={0}:sum={1}<br/>", i, sum));
      Response.Flush();
      System.Threading.Thread.Sleep(5000);
    }
  }
  // Difference finding method 
  protected void CalcGap()
  {
    long gap = 0;
    for (long i = 100; i >= 0; i--)
    {
      gap = i - 1;
      Response.Write(string.Format("Thread2-->i={0}:gap={1}<br/>", i, gap));
      Response.Flush();
      System.Threading.Thread.Sleep(1000);
    }
  }
}

For more readers interested in C # related content, please check out the topics on this site: "Summary of Thread Use Techniques in C # Programming", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: