国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#多線程傳遞參數(shù)及任務(wù)用法示例

2019-10-29 21:26:32
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#多線程傳遞參數(shù)及任務(wù)用法。分享給大家供大家參考,具體如下:

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("這是主線程");      DateTime dtStart = DateTime.Now;      for (int i = 0; i < 100; i++)      {        Student s = new Student();        s.name = "張三" + i;        s.sex = "男";        s.age = 28;        Thread t = new Thread(ThreadWithParas);        t.Start(s); //要給線程傳遞數(shù)據(jù),需某個(gè)存儲(chǔ)數(shù)據(jù)的類或結(jié)構(gòu)      }      DateTime dtEnd = DateTime.Now;      TimeSpan span = (TimeSpan)(dtEnd - dtStart);      Console.ReadLine();      Console.WriteLine("運(yùn)行的時(shí)間 " + span.Milliseconds);      Console.ReadLine();    }    static void ThreadWithParas(object o)    {      Student s = o as Student;      Console.WriteLine("這是分線程" + 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)    {      //任務(wù)層次結(jié)構(gòu)      Task parent = new Task(ParentTask);      parent.Start();      Thread.Sleep(2000);      Console.WriteLine(parent.Status);      Thread.Sleep(4000);      Console.WriteLine(parent.Status);      Console.ReadLine();    }    //父任務(wù)    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);    }    //子任務(wù)    static void ChildTask()    {      Console.WriteLine("child");      Thread.Sleep(5000);      Console.WriteLine("child finished");    }  }}


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雷州市| 兴山县| 腾冲县| 衡阳市| 元谋县| 黎城县| 伽师县| 湛江市| 蓝山县| 安阳市| 鄂伦春自治旗| 昭平县| 沧源| 潮州市| 宜都市| 宁武县| 朝阳县| 紫阳县| 金山区| 呼和浩特市| 芦山县| 潞城市| 吉木萨尔县| 体育| 紫云| 芦溪县| 淅川县| 宾阳县| 新乡县| 吉安县| 商城县| 铁力市| 长治市| 电白县| 蓬溪县| 浪卡子县| 邛崃市| 富裕县| 宜宾市| 巧家县| 攀枝花市|