最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年04月24日 17:14:48   作者:劍蕭舞蝶  
這篇文章主要介紹了C#程序中的單線程與多線程設(shè)計(jì)的簡(jiǎn)單對(duì)比,通過(guò)實(shí)際的代碼演示可以清晰看出多線程并發(fā)來(lái)避免單線程阻塞問(wèn)題的特點(diǎn),需要的朋友可以參考下

多線程概念

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ì)共享資源訪問(wèn)會(huì)互相影響,所以得解決爭(zhēng)用共享資源的問(wèn)題,線程太多,也會(huì)導(dǎo)致控制起來(lái)更復(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)文章

最新評(píng)論

清水河县| 盐山县| 泽普县| 乌鲁木齐市| 柘城县| 醴陵市| 祁门县| 奉化市| 上犹县| 仪陇县| 梧州市| 宿迁市| 简阳市| 祁阳县| 苏尼特右旗| 栾城县| 仙桃市| 武威市| 县级市| 马尔康县| 井陉县| 光山县| 洪泽县| 深州市| 铁岭市| 齐齐哈尔市| 鹿邑县| 微山县| 松潘县| 连城县| 厦门市| 开化县| 观塘区| 灵石县| 平昌县| 广平县| 遂平县| 尼勒克县| 阳信县| 墨脱县| 三明市|