簡(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)線程
}
}
}
相關(guān)文章
C# listview 點(diǎn)擊列頭排序的實(shí)例
下面小編就為大家?guī)硪黄狢# listview 點(diǎn)擊列頭排序的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
詳解如何通過C#/VB.NET調(diào)整PDF文檔頁(yè)邊距
PDF邊距是頁(yè)面主要內(nèi)容區(qū)域和頁(yè)面邊緣之間的距離。與Word頁(yè)邊距不同,PDF文檔的頁(yè)邊距很難更改。本文將介紹如何在不更改頁(yè)面大小的情況下使用C#/VB.NET?代碼調(diào)整PDF文檔的頁(yè)邊距,需要的可以參考一下2023-04-04
Winform使用FTP實(shí)現(xiàn)自動(dòng)更新
這篇文章主要為大家詳細(xì)介紹了Winform使用FTP實(shí)現(xiàn)自動(dòng)更新,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
NGUI實(shí)現(xiàn)滑動(dòng)翻頁(yè)效果實(shí)例代碼
本文通過一段實(shí)例代碼給大家介紹NGUI實(shí)現(xiàn)滑動(dòng)翻頁(yè)效果,代碼簡(jiǎn)單易懂,對(duì)ngui 滑動(dòng)翻頁(yè)相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04
C#基于簡(jiǎn)單工廠模式實(shí)現(xiàn)的計(jì)算器功能示例
這篇文章主要介紹了C#基于簡(jiǎn)單工廠模式實(shí)現(xiàn)的計(jì)算器功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了C#使用工廠模式的數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
C# OpenVINO實(shí)現(xiàn)圖片旋轉(zhuǎn)角度檢測(cè)
這篇文章主要為大家詳細(xì)介紹了C#?OpenVINO如何實(shí)現(xiàn)圖片旋轉(zhuǎn)角度檢測(cè),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
C#實(shí)現(xiàn)Post數(shù)據(jù)或文件到指定的服務(wù)器進(jìn)行接收
這篇文章主要為大家詳細(xì)介紹了如何通過C#實(shí)現(xiàn)Post數(shù)據(jù)或文件到指定的服務(wù)器進(jìn)行接收,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考下2024-03-03
WPF+DiffPlex實(shí)現(xiàn)文本比對(duì)工具
現(xiàn)行的文本編輯器大多都具備文本查詢的能力,但是并不能直觀的告訴用戶兩段文字的細(xì)微差異,所以對(duì)比工具在某種情況下,就起到了很便捷的效率。本文將利用DiffPlex實(shí)現(xiàn)簡(jiǎn)易的文本比對(duì)工具,需要的可以參考一下2022-11-11

