C # Multithreading pass parameters and Task Usage Sample _c# Tutorial

Source: Internet
Author: User
Tags datetime readline

The examples in this article describe C # Multithreading pass parameters and Task usage. Share to everyone for your reference, specific as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
      Namespace Consolesample {class Program {static void Main (string[] args) {Console.WriteLine ("This is the main thread");
      DateTime Dtstart = DateTime.Now;
        for (int i = 0; i < i++) {Student s = new Student ();
        S.name = "John" + I;
        S.sex = "male";
        S.age = 28;
        Thread t = new thread (Threadwithparas); T.start (s);
      To pass data to a thread, you need a class or struct that stores the data} DateTime dtend = DateTime.Now;
      TimeSpan span = (TimeSpan) (Dtend-dtstart);
      Console.ReadLine ();
      Console.WriteLine ("Running Time" + span.milliseconds);
    Console.ReadLine ();
      The static void Threadwithparas (object o) {Student s = o as Student;
    Console.WriteLine ("This is divided into threads" + Thread.CurrentThread.ManagedThreadId + "" + S.name + "---" + s.sex + "---" + s.age);
    } public class Student {public string name; Public String sex;
  public int age;

 }
}
 using System; using System.Collections.Generic; using System.Linq; using System.Text;
Using System.Threading;
Using System.Threading.Tasks;  Namespace Consolesample {class Program {static void Main (string[] args) {//task-Hierarchy Task parent =
      New Task (Parenttask); Parent.
      Start ();
      Thread.Sleep (2000); Console.WriteLine (parent.
      Status);
      Thread.Sleep (4000); Console.WriteLine (parent.
      Status);
    Console.ReadLine ();
      }//Parent task static void Parenttask () {Console.WriteLine ("task ID {0}", Task.currentid); Task child = new Task (childtask);
      , taskcreationoptions.detachedfromparent); Child.
      Start ();
      Thread.Sleep (1000);
      Console.WriteLine ("Parent started child");
    Thread.Sleep (3000);
      }//subtask static void Childtask () {Console.WriteLine ("Child");
      Thread.Sleep (5000);
    Console.WriteLine ("Child Finished"); }
  }
}

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.