SoFunction
Updated on 2025-03-08

Examples of C# multi-threaded parameters and task usage

This article describes the usage of C# multi-threaded parameters and tasks. Share it for your reference, as follows:

using System;
using ;
using ;
using ;
using ;
namespace ConsoleSample
{
  class Program
  {
    static void Main(string[] args)
    {
      ("This is the main thread");
      DateTime dtStart = ;
      for (int i = 0; i < 100; i++)
      {
        Student s = new Student();
         = "Zhang San" + i;
         = "male";
         = 28;
        Thread t = new Thread(ThreadWithParas);
        (s); //To pass data to threads, a class or structure that needs to store data is required.      }
      DateTime dtEnd = ;
      TimeSpan span = (TimeSpan)(dtEnd - dtStart);
      ();
      ("Time to run" + );
      ();
    }
    static void ThreadWithParas(object o)
    {
      Student s = o as Student;
      ("This is a threading" +  + " " +  + "---" +  + "---" + );
    }
  }
  public class Student
  {
    public string name;
    public string sex;
    public int age;
  }
}

using System;
using ;
using ;
using ;
using ;
using ;
namespace ConsoleSample
{
  class Program
  {
    static void Main(string[] args)
    {
      //Task Hierarchy      Task parent = new Task(ParentTask);
      ();
      (2000);
      ();
      (4000);
      ();
      ();
    }
    // Parent task    static void ParentTask()
    {
      ("task id {0}", );
      Task child = new Task(ChildTask); // , );
      ();
      (1000);
      ("parent started child");
      // (3000);
    }
    //Subtask    static void ChildTask()
    {
      ("child");
      (5000);
      ("child finished");
    }
  }
}

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

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