簡(jiǎn)單對(duì)比C#程序中的單線程與多線程設(shè)計(jì)
多線程概念
1.一個(gè)正在運(yùn)行的應(yīng)用程序在操作系統(tǒng)中被視為一個(gè)進(jìn)程,進(jìn)程可以包括多個(gè)線程。線程是操作系統(tǒng)分配處理器時(shí)間的基本單位
2.應(yīng)用程序域是指進(jìn)行錯(cuò)誤隔離和安全隔離,在CLR中運(yùn)行,每個(gè)程序域都是單個(gè)線程啟動(dòng),但該程序域中的代碼可以創(chuàng)建附加應(yīng)用程序域和附加線程
3.多線程的優(yōu)點(diǎn)在于一個(gè)線程阻塞的時(shí)候,CUP可以運(yùn)行其他的線程而不需要等待,這樣大大的提高了程序的執(zhí)行效率。而缺點(diǎn)在于線程需要占用內(nèi)存,線程越多占用的內(nèi)存就多,多線程需要協(xié)調(diào)和管理,所以需要占用CPU時(shí)間以便跟蹤線程,線程之間對(duì)共享資源訪問會(huì)互相影響,所以得解決爭(zhēng)用共享資源的問題,線程太多,也會(huì)導(dǎo)致控制起來更復(fù)雜,最終導(dǎo)致很多程序的缺陷。
4.一個(gè)進(jìn)程可以創(chuàng)建多個(gè)線程以執(zhí)行與該進(jìn)程關(guān)聯(lián)的部分程序代碼,線程使用Tread處理
C#單線程與多線程對(duì)比:
單線程:
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();//啟動(dòng)線程 } } }
多線程、并發(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();//啟動(dòng)線程 Thread tr1 = new Thread(new ThreadStart(stratwrite1));//創(chuàng)建第二個(gè)線程 tr1.Start();//啟動(dòng)線程 } } }
上一篇:C#利用原圖和水印圖的重疊簡(jiǎn)單實(shí)現(xiàn)水印的方法
欄 目:C#教程
下一篇:C#抓取網(wǎng)頁數(shù)據(jù) 解析標(biāo)題描述圖片等信息 去除HTML標(biāo)簽
本文標(biāo)題:簡(jiǎn)單對(duì)比C#程序中的單線程與多線程設(shè)計(jì)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6561.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單的Login窗口實(shí)例
- 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
- 01-10Winform消除button按下出現(xiàn)的虛線簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10winform簡(jiǎn)單緩存類實(shí)例
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#圓角窗體簡(jiǎn)單實(shí)現(xiàn)方法
- 01-10C#實(shí)現(xiàn)程序等待延遲執(zhí)行的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什