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

首頁 > 編程 > C# > 正文

簡單對比C#程序中的單線程與多線程設(shè)計(jì)

2020-01-24 01:11:15
字體:
供稿:網(wǎng)友

多線程概念

1.一個正在運(yùn)行的應(yīng)用程序在操作系統(tǒng)中被視為一個進(jìn)程,進(jìn)程可以包括多個線程。線程是操作系統(tǒng)分配處理器時間的基本單位
2.應(yīng)用程序域是指進(jìn)行錯誤隔離和安全隔離,在CLR中運(yùn)行,每個程序域都是單個線程啟動,但該程序域中的代碼可以創(chuàng)建附加應(yīng)用程序域和附加線程
3.多線程的優(yōu)點(diǎn)在于一個線程阻塞的時候,CUP可以運(yùn)行其他的線程而不需要等待,這樣大大的提高了程序的執(zhí)行效率。而缺點(diǎn)在于線程需要占用內(nèi)存,線程越多占用的內(nèi)存就多,多線程需要協(xié)調(diào)和管理,所以需要占用CPU時間以便跟蹤線程,線程之間對共享資源訪問會互相影響,所以得解決爭用共享資源的問題,線程太多,也會導(dǎo)致控制起來更復(fù)雜,最終導(dǎo)致很多程序的缺陷。
4.一個進(jìn)程可以創(chuàng)建多個線程以執(zhí)行與該進(jìn)程關(guān)聯(lián)的部分程序代碼,線程使用Tread處理

C#單線程與多線程對比:

單線程:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading;  namespace Stockes {   public partial class DeletgateThread : Form   {     public DeletgateThread()     {       InitializeComponent();       CheckForIllegalCrossThreadCalls = false;//允許跨線程調(diào)用     }     public delegate void writeTxt(char chr);//定義委托     public writeTxt writetxt;//聲明委托     public void write(string str, writeTxt writes)//使用委托     {       for (int i = 0; i < str.Length; i++)       {         writes(str[i]);         DateTime now = DateTime.Now;         while (now.AddSeconds(1) > DateTime.Now) { }       }     }     private void text1(char chr)     {       textBox1.AppendText(chr.ToString());     }     public void text2(char chr)     {       textBox2.AppendText(chr.ToString());     }     private void stratWrite()     {           if (checkBox1.Checked)       {         textBox1.Clear();         groupBox4.Text = "正在運(yùn)行。。";         groupBox2.Refresh();         writetxt = new writeTxt(text1);         write(textBox3.Text.Trim(),writetxt);       }       if(checkBox2.Checked)       {         textBox2.Clear();         groupBox5.Text = "正在運(yùn)行。。";         groupBox3.Refresh();         writetxt = new writeTxt(text2);         write(textBox3.Text.Trim(),writetxt);       }     }     private void button1_Click(object sender, EventArgs e)     {       Thread tr = new Thread(new ThreadStart(stratWrite));//創(chuàng)建線程       tr.Start();//啟動線程     }        } }  

 
多線程、并發(fā)任務(wù): 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading;  namespace Stockes {   public partial class DeletgateThread : Form   {     public DeletgateThread()     {       InitializeComponent();       CheckForIllegalCrossThreadCalls = false;//允許跨線程調(diào)用     }     public delegate void writeTxt(char chr);//定義委托     public writeTxt writetxt;//聲明委托     public void write(string str, writeTxt writes)//使用委托     {       for (int i = 0; i < str.Length; i++)       {         writes(str[i]);         DateTime now = DateTime.Now;         while (now.AddSeconds(1) > DateTime.Now) { }       }     }     private void text1(char chr)     {       textBox1.AppendText(chr.ToString());     }     public void text2(char chr)     {       textBox2.AppendText(chr.ToString());     }     private void stratWrite()     {       if (checkBox1.Checked)       {         textBox1.Clear();         textBox1.Refresh();         groupBox4.Text = "正在運(yùn)行。。";         groupBox2.Refresh();         writetxt = new writeTxt(text1);         write(textBox3.Text.Trim(),writetxt);       }     }     private void stratwrite1()     {       if (checkBox2.Checked)       {         textBox2.Clear();         textBox2.Refresh();         groupBox5.Text = "正在運(yùn)行。。";         groupBox3.Refresh();         writetxt = new writeTxt(text2);         write(textBox3.Text.Trim(), writetxt);       }     }     private void button1_Click(object sender, EventArgs e)     {       Thread tr = new Thread(new ThreadStart(stratWrite));//創(chuàng)建線程       tr.Start();//啟動線程       Thread tr1 = new Thread(new ThreadStart(stratwrite1));//創(chuàng)建第二個線程       tr1.Start();//啟動線程     }        } } 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南京市| 建平县| 蒙阴县| 平原县| 阜新市| 内黄县| 康保县| 罗城| 张家港市| 赞皇县| 临潭县| 元谋县| 邢台县| 哈尔滨市| 尉氏县| 永修县| 太和县| 榆社县| 申扎县| 河南省| 西藏| 五华县| 太湖县| 德格县| 闸北区| 襄樊市| 元江| 通州市| 茶陵县| 秀山| 白城市| 东乡县| 长垣县| 阿拉善盟| 湖南省| 余江县| 商都县| 青海省| 繁昌县| 浑源县| 城口县|