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.