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

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

C#下用P2P技術(shù)實(shí)現(xiàn)點(diǎn)對(duì)點(diǎn)聊天

2024-07-21 02:18:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

以前在使用vb來(lái)實(shí)現(xiàn)多線程的時(shí)候,發(fā)現(xiàn)有一定的難度。雖然也有這樣那樣的方法,但都不盡人意,但在c#中,要編寫(xiě)多線程應(yīng)用程序卻相當(dāng)?shù)暮?jiǎn)單。這篇文章將作簡(jiǎn)要的介紹,以起到拋磚引玉的作用!
.net將關(guān)于多線程的功能定義在system.threading名字空間中。因此,要使用多線程,必須先聲明引用此名字空間(using system.threading;)。
即使你沒(méi)有編寫(xiě)多線程應(yīng)用程序的經(jīng)驗(yàn),也可能聽(tīng)說(shuō)過(guò)“啟動(dòng)線程”“殺死線程”這些詞,其實(shí)除了這兩個(gè)外,涉及多線程方面的還有諸如“暫停線程”“優(yōu)先級(jí)”“掛起線程”“恢復(fù)線程”等等。下面將一個(gè)一個(gè)的解釋。
a.啟動(dòng)線程
顧名思義,“啟動(dòng)線程”就是新建并啟動(dòng)一個(gè)線程的意思,如下代碼可實(shí)現(xiàn):
thread thread1 = new thread(new threadstart( count));
其中的 count 是將要被新線程執(zhí)行的函數(shù)。
b.殺死線程
“殺死線程”就是將一線程斬草除根,為了不白費(fèi)力氣,在殺死一個(gè)線程前最好先判斷它是否還活著(通過(guò) isalive 屬性),然后就可以調(diào)用 abort 方法來(lái)殺死此線程。
c.暫停線程
它的意思就是讓一個(gè)正在運(yùn)行的線程休眠一段時(shí)間。如 thread.sleep(1000); 就是讓線程休眠1秒鐘。
d.優(yōu)先級(jí)
這個(gè)用不著解釋了。thread類(lèi)中有一個(gè)threadpriority屬性,它用來(lái)設(shè)置優(yōu)先級(jí),但不能保證操作系統(tǒng)會(huì)接受該優(yōu)先級(jí)。一個(gè)線程的優(yōu)先級(jí)可分為5種:normal, abovenormal, belownormal, highest, lowest。具體實(shí)現(xiàn)例子如下:
thread.priority = threadpriority.highest;
e.掛起線程
thread類(lèi)的suspend方法用來(lái)掛起線程,知道調(diào)用resume,此線程才可以繼續(xù)執(zhí)行。如果線程已經(jīng)掛起,那就不會(huì)起作用。
if (thread.threadstate = threadstate.running)
{
thread.suspend();
}
f.恢復(fù)線程
用來(lái)恢復(fù)已經(jīng)掛起的線程,以讓它繼續(xù)執(zhí)行,如果線程沒(méi)掛起,也不會(huì)起作用。
if (thread.threadstate = threadstate.suspended)
{
thread.resume();
}
下面將列出一個(gè)例子,以說(shuō)明簡(jiǎn)單的線程處理功能。此例子來(lái)自于幫助文檔。
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();
}
}

此代碼產(chǎn)生的輸出類(lèi)似如下內(nèi)容:

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.






[1]

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 东山县| 大埔区| 新密市| 措美县| 遵化市| 衡水市| 富锦市| 个旧市| 南投市| 盘山县| 商丘市| 乌海市| 彭泽县| 嫩江县| 东港市| 汤原县| 久治县| 太谷县| 亳州市| 黄骅市| 依兰县| 抚松县| 庆安县| 治多县| 时尚| 铜川市| 共和县| 精河县| 文登市| 安阳市| 乐亭县| 楚雄市| 三都| 松桃| 鸡东县| 米泉市| 大姚县| 临武县| 阿拉善盟| 从江县| 南充市|