C#编程-147:线程基础
2024-06-25 09:50:39 阅读次数:30
编程开发
启动线程:
- ThreadStart ts = new ThreadStart(method);//创建委托实例
- Thread t = new Thread(ts);//创建线程
- t.Start();//启动线程
线程休眠
- t.Suspend();//线程挂起
- Thread.Sleep(1000);//线程休眠
- t.Resume();//线程继续
属性:IsAlive,判断线程当前执行状态
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace ThreadPriorityTest
- {
- class Program
- {
- public static void method()
- {
- Console.WriteLine("线程名称:{0},线程优先级:{1}",
- Thread.CurrentThread.Name.ToString(),
- Thread.CurrentThread.Priority.ToString());
-
- }
- static void Main(string[] args)
- {
- ThreadStart ts1 = new ThreadStart(method);//创建委托实例
- ThreadStart ts2 = new ThreadStart(method);
- Thread t1 = new Thread(ts1);//创建线程
- Thread t2 = new Thread(ts2);
- t1.Name = "线程1";
- t2.Name = "线程2";
- t1.Priority = ThreadPriority.Highest;
- t2.Priority = ThreadPriority.BelowNormal;
- t1.Start();
- t2.Start();
- Console.ReadKey();
-
- }
- }
- }
版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://blog.51cto.com/mouday/3047198,作者:彭世瑜,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。
上一篇:Python编程:calendar日历模块入门
下一篇:C#编程-99:索引器实例QQ状态