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

学习网站可以在http://www.yesky.com/331/215831.shtml
里看到所有文章

a.在C#中要使用线程要引用System.Threading;

None.gif using  System.Threading;

b.在C#可以对程进行命名

None.gif Thread _thrd  =   new  Thread( new  ThreadStart(dot.gif));
None.gif_thrd.Name 
=   " thisthrd.name "

c.可以通过Thread的static属性获取当前线程
None.gif Thread.CurrentThread

d.操作线程的几个重要的方法

Start():启动线程
Sleep(int):静态方法,暂停当前线程指定的毫秒数
Abort():通常使用该方法来终止一个线程
Suspend():该方法并不终止未完成的线程,它仅仅挂起线程,以后还可恢复。
Resume():恢复被Suspend()方法挂起的线程的执行

e. sample

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Threading;
None.gif
None.gif
namespace  ThreadOperation
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Alpha
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Int32 i;
InBlock.gif        
public Alpha()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            i 
= 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void Beta()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(i.ToString() 
+ ":Alpha.Beta is running out of its own thread");
InBlock.gif                i
++;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static int Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Thread.CurrentThread.Name 
= "System Thread";
InBlock.gif            Console.WriteLine(
"Thread Start/Stop/Join sample");
InBlock.gif            Alpha oAlpha 
= new Alpha();
InBlock.gif            Thread oThread 
= new Thread(new ThreadStart(oAlpha.Beta));
InBlock.gif            oThread.Start();
InBlock.gif            oThread.Name 
= "Alpha Thread";
InBlock.gif            
while (!oThread.IsAlive) Console.WriteLine("has dead");
InBlock.gif            Console.WriteLine(
"Current Thread:" + Thread.CurrentThread.Name);
InBlock.gif            Thread.Sleep(
1);
InBlock.gif            Console.WriteLine(
"oThread.ThreadState:"+oThread.ThreadState);
InBlock.gif            oThread.Abort();
InBlock.gif            Console.WriteLine(
"oThread.ThreadState:" + oThread.ThreadState);
InBlock.gif            oThread.Join();
InBlock.gif            Console.WriteLine(
"oThread.ThreadState:" + oThread.ThreadState);
InBlock.gif            Console.WriteLine();
InBlock.gif            Console.WriteLine(
"Alpha.Beta has finished");
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"Try to restart the Alpha.Beta thread");
InBlock.gif                oThread.Start();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (ThreadStateException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"oThread.ThreadState:" + oThread.ThreadState);
InBlock.gif                Console.Write(
"ThreadStateException trying to restart Alpha.Beta. ");
InBlock.gif                Console.WriteLine(
"Expected since aborted threads cannot be restarted.");
InBlock.gif                Console.ReadLine();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return 0;
InBlock.gif
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值