Csharp/C#教程:简单对比C#程序中的单线程与多线程设计分享

多线程概念

1.一个正在运行的应用程序在操作系统中被视为一个进程,进程可以包括多个线程。线程是操作系统分配处理器时间的基本单位
2.应用程序域是指进行错误隔离和安全隔离,在CLR中运行,每个程序域都是单个线程启动,但该程序域中的代码可以创建附加应用程序域和附加线程
3.多线程的优点在于一个线程阻塞的时候,CUP可以运行其他的线程而不需要等待,这样大大的提高了程序的执行效率。而缺点在于线程需要占用内存,线程越多占用的内存就多,多线程需要协调和管理,所以需要占用CPU时间以便跟踪线程,线程之间对共享资源访问会互相影响,所以得解决争用共享资源的问题,线程太多,也会导致控制起来更复杂,最终导致很多程序的缺陷。
4.一个进程可以创建多个线程以执行与该进程关联的部分程序代码,线程使用Tread处理

C#单线程与多线程对比:

单线程:

usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Threading; namespaceStockes { publicpartialclassDeletgateThread:Form { publicDeletgateThread() { InitializeComponent(); CheckForIllegalCrossThreadCalls=false;//允许跨线程调用 } publicdelegatevoidwriteTxt(charchr);//定义委托 publicwriteTxtwritetxt;//声明委托 publicvoidwrite(stringstr,writeTxtwrites)//使用委托 { for(inti=0;i<str.Length;i++) { writes(str[i]); DateTimenow=DateTime.Now; while(now.AddSeconds(1)>DateTime.Now){} } } privatevoidtext1(charchr) { textBox1.AppendText(chr.ToString()); } publicvoidtext2(charchr) { textBox2.AppendText(chr.ToString()); } privatevoidstratWrite() { if(checkBox1.Checked) { textBox1.Clear(); groupBox4.Text="正在运行。。"; groupBox2.Refresh(); writetxt=newwriteTxt(text1); write(textBox3.Text.Trim(),writetxt); } if(checkBox2.Checked) { textBox2.Clear(); groupBox5.Text="正在运行。。"; groupBox3.Refresh(); writetxt=newwriteTxt(text2); write(textBox3.Text.Trim(),writetxt); } } privatevoidbutton1_Click(objectsender,EventArgse) { Threadtr=newThread(newThreadStart(stratWrite));//创建线程 tr.Start();//启动线程 } } }

 
多线程、并发任务: 

usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Threading; namespaceStockes { publicpartialclassDeletgateThread:Form { publicDeletgateThread() { InitializeComponent(); CheckForIllegalCrossThreadCalls=false;//允许跨线程调用 } publicdelegatevoidwriteTxt(charchr);//定义委托 publicwriteTxtwritetxt;//声明委托 publicvoidwrite(stringstr,writeTxtwrites)//使用委托 { for(inti=0;i<str.Length;i++) { writes(str[i]); DateTimenow=DateTime.Now; while(now.AddSeconds(1)>DateTime.Now){} } } privatevoidtext1(charchr) { textBox1.AppendText(chr.ToString()); } publicvoidtext2(charchr) { textBox2.AppendText(chr.ToString()); } privatevoidstratWrite() { if(checkBox1.Checked) { textBox1.Clear(); textBox1.Refresh(); groupBox4.Text="正在运行。。"; groupBox2.Refresh(); writetxt=newwriteTxt(text1); write(textBox3.Text.Trim(),writetxt); } } privatevoidstratwrite1() { if(checkBox2.Checked) { textBox2.Clear(); textBox2.Refresh(); groupBox5.Text="正在运行。。"; groupBox3.Refresh(); writetxt=newwriteTxt(text2); write(textBox3.Text.Trim(),writetxt); } } privatevoidbutton1_Click(objectsender,EventArgse) { Threadtr=newThread(newThreadStart(stratWrite));//创建线程 tr.Start();//启动线程 Threadtr1=newThread(newThreadStart(stratwrite1));//创建第二个线程 tr1.Start();//启动线程 } } } 您可能感兴趣的文章:C#在Unity游戏开发中进行多线程编程的方法C#线程处理系列之线程池中的I/O线程解析C#多线程编程中异步多线程的实现及线程池的使用C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法C#基于委托实现多线程之间操作的方法C#实现向多线程传参的三种方式实例分析C#线程队列用法实例分析C#停止线程的方法C#中前台线程和后台线程的区别与联系C#在子线程中更新窗口部件的写法

标签: 线程 多线程 单线程 程序

利用C++11原子量如何实现自旋锁详解

详解C++句柄类

上述就是C#学习教程:简单对比C#程序中的单线程与多线程设计分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/907101.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐