簡單對比C#程序中的單線程與多線程設計
多線程概念
1.一個正在運行的應用程序在操作系統(tǒng)中被視為一個進程,進程可以包括多個線程。線程是操作系統(tǒng)分配處理器時間的基本單位
2.應用程序域是指進行錯誤隔離和安全隔離,在CLR中運行,每個程序域都是單個線程啟動,但該程序域中的代碼可以創(chuàng)建附加應用程序域和附加線程
3.多線程的優(yōu)點在于一個線程阻塞的時候,CUP可以運行其他的線程而不需要等待,這樣大大的提高了程序的執(zhí)行效率。而缺點在于線程需要占用內(nèi)存,線程越多占用的內(nèi)存就多,多線程需要協(xié)調(diào)和管理,所以需要占用CPU時間以便跟蹤線程,線程之間對共享資源訪問會互相影響,所以得解決爭用共享資源的問題,線程太多,也會導致控制起來更復雜,最終導致很多程序的缺陷。
4.一個進程可以創(chuàng)建多個線程以執(zhí)行與該進程關聯(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 = "正在運行。。"; 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));//創(chuàng)建線程 tr.Start();//啟動線程 } } }
多線程、并發(fā)任務:
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 = "正在運行。。"; 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));//創(chuàng)建線程 tr.Start();//啟動線程 Thread tr1 = new Thread(new ThreadStart(stratwrite1));//創(chuàng)建第二個線程 tr1.Start();//啟動線程 } } }
相關文章
詳解如何通過C#/VB.NET調(diào)整PDF文檔頁邊距
PDF邊距是頁面主要內(nèi)容區(qū)域和頁面邊緣之間的距離。與Word頁邊距不同,PDF文檔的頁邊距很難更改。本文將介紹如何在不更改頁面大小的情況下使用C#/VB.NET?代碼調(diào)整PDF文檔的頁邊距,需要的可以參考一下2023-04-04C# OpenVINO實現(xiàn)圖片旋轉(zhuǎn)角度檢測
這篇文章主要為大家詳細介紹了C#?OpenVINO如何實現(xiàn)圖片旋轉(zhuǎn)角度檢測,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-02-02C#實現(xiàn)Post數(shù)據(jù)或文件到指定的服務器進行接收
這篇文章主要為大家詳細介紹了如何通過C#實現(xiàn)Post數(shù)據(jù)或文件到指定的服務器進行接收,文中的示例代碼講解詳細,需要的小伙伴可以參考下2024-03-03