簡略比較C#法式中的單線程與多線程設計。本站提示廣大學習愛好者:(簡略比較C#法式中的單線程與多線程設計)文章只能為提供參考,不一定能成為您想要的結果。以下是簡略比較C#法式中的單線程與多線程設計正文
多線程概念
1.一個正在運轉的運用法式在操作體系中被視為一個過程,過程可以包含多個線程。線程是操作體系分派處置器時光的根本單元
2.運用法式域是指停止毛病隔離和平安隔離,在CLR中運轉,每一個法式域都是單個線程啟動,但該法式域中的代碼可以創立附加運用法式域和附加線程
3.多線程的長處在於一個線程壅塞的時刻,CUP可以運轉其他的線程而不須要期待,如許年夜年夜的進步了法式的履行效力。而缺陷在於線程須要占用內存,線程越多占用的內存就多,多線程須要調和和治理,所以須要占用CPU時光以便跟蹤線程,線程之間對同享資本拜訪會相互影響,所以得處理爭用同享資本的成績,線程太多,也會招致掌握起來更龐雜,終究招致許多法式的缺點。
4.一個過程可以創立多個線程以履行與該過程聯系關系的部門法式代碼,線程應用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;//許可跨線程挪用 } 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 = "正在運轉。。"; groupBox2.Refresh(); writetxt = new writeTxt(text1); write(textBox3.Text.Trim(),writetxt); } if(checkBox2.Checked) { textBox2.Clear(); groupBox5.Text = "正在運轉。。"; 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));//創立線程 tr.Start();//啟動線程 } } }
多線程、並發義務:
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;//許可跨線程挪用 } 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 = "正在運轉。。"; groupBox2.Refresh(); writetxt = new writeTxt(text1); write(textBox3.Text.Trim(),writetxt); } } private void stratwrite1() { if (checkBox2.Checked) { textBox2.Clear(); textBox2.Refresh(); groupBox5.Text = "正在運轉。。"; 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));//創立線程 tr.Start();//啟動線程 Thread tr1 = new Thread(new ThreadStart(stratwrite1));//創立第二個線程 tr1.Start();//啟動線程 } } }