C#多线程学习笔记(一)

Thread C#的线程类:

Thrd = new Thread(this.Run); Run就是我们线程要执行的函数

函数类型必须是这样,void Run();

Thrd = new Thread(this.Run);
Thrd.Name = name; //线程的名字
Thrd.Start(); //就是开始线程

 

Thread.Sleep(250);//休眠250ms

Thrd.Suspend();//线程挂起

Thrd.Resume();//恢复

Thrd.Join(); //等待线程执行结束以后才继续执行下面的语句

 1 namespace Thread_CS
 2 {
 3     class MyThread
 4     {
 5         public Thread Thrd;
 6         public MyThread(string name)
 7         {
 8             Thrd = new Thread(this.Run);
 9             Thrd.Name = name;
10             Thrd.Start();
11         }
12         // This is the entry point for thread.
13         void Run()
14         {
15             try
16             {
17                 Console.WriteLine(Thrd.Name + " starting.");
18                 for (int i = 1; i <= 100; i++)
19                 {
20                     Console.Write(i + " ");
21                     if ((i % 10) == 0)
22                     {
23                         Console.WriteLine();
24                         Thread.Sleep(250);
25                     }
26                 }
27                 Console.WriteLine(Thrd.Name + " exiting normally.");
28             }
29             catch (ThreadAbortException exc)
30             {
31                 Console.WriteLine("Thread aborting, code is " +
32                                    exc.ExceptionState);
33 //                 Thread.ResetAbort();
34             }
35 //             Console.WriteLine(Thrd.Name + " exiting normally.");
36         }
37     }
38 
39     class Program
40     {
41         static void Main(string[] args)
42         {
43             MyThread mt1 = new MyThread("My Thread");
44             Thread.Sleep(1000); // let child thread start executing
45             
46             mt1.Thrd.Suspend();
47             Console.WriteLine(mt1.Thrd.ThreadState);
48             mt1.Thrd.Resume();
49             mt1.Thrd.Join(); // wait for thread to terminate
50             Console.WriteLine("Main thread terminating.");
51 
52         }
53     }
54 }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值