王朝网络
分享
 
 
 

C#中编写多线程(1)——起步

王朝c#·作者佚名  2006-03-08
宽屏版  字体: |||超大  

在C#中早都听说这个东西了,但是以前一直没有使用过,现在第一次尝试,来冲冲电。

C#中编写多线程

.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。

即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。

a.启动线程

顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:

Thread thread1 = new Thread(new ThreadStart( Count));

其中的 Count 是将要被新线程执行的函数。

b.杀死线程

“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。

c.暂停线程

它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。

d.优先级

这个用不着解释了。Thread类中有一个ThreadPriority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可 分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下:

thread.Priority = ThreadPriority.Highest;

e.挂起线程

Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。

if (thread.ThreadState = ThreadState.Running)

{

thread.Suspend();

}

f.恢复线程

用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。

if (thread.ThreadState = ThreadState.Suspended)

{

thread.Resume();

}

下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。

using System;

using System.Threading;

// Simple threading scenario: Start a static method running

// on a second thread.

public class ThreadExample {

// The ThreadProc method is called when the thread starts.

// It loops ten times, writing to the console and yielding

// the rest of its time slice each time, and then ends.

public static void ThreadProc() {

for (int i = 0; i < 10; i++) {

Console.WriteLine("ThreadProc: {0}", i);

// Yield the rest of the time slice.

Thread.Sleep(0);

}

}

public static void Main() {

Console.WriteLine("Main thread: Start a second thread.");

// The constructor for the Thread class requires a ThreadStart

// delegate that represents the method to be executed on the

// thread. C# simplifies the creation of this delegate.

Thread t = new Thread(new ThreadStart(ThreadProc));

// Start ThreadProc. On a uniprocessor, the thread does not get

// any processor time until the main thread yields. Uncomment

// the Thread.Sleep that follows t.Start() to see the difference.

t.Start();

//Thread.Sleep(0);

for (int i = 0; i < 4; i++) {

Console.WriteLine("Main thread: Do some work.");

Thread.Sleep(0);

}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");

t.Join();

Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");

Console.ReadLine();

}

}

此代码产生的输出类似如下内容:

Main thread: Start a second thread.

Main thread: Do some work.

ThreadProc: 0

Main thread: Do some work.

ThreadProc: 1

Main thread: Do some work.

ThreadProc: 2

Main thread: Do some work.

ThreadProc: 3

Main thread: Call Join(), to wait until ThreadProc ends.

ThreadProc: 4

ThreadProc: 5

ThreadProc: 6

ThreadProc: 7

ThreadProc: 8

ThreadProc: 9

Main thread: ThreadProc.Join has returned. Press Enter to end program.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有