线程:主线程、子线程 同步线程、异步线程 单线程、多线程 System.Threading与System.Windows.Threading
入门--------------------------------------------------------------------------------
概述与概念
一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建。
创建和开始使用多线程
public Window1()
{
//主线程
//Code……
//使用匿名方法来启动子线程
Thread t = new Thread(delegate() {Code……});
t.Start();
}
子进程创建实例
//1.创建子线程
public Window1()
{
//创建子进程
Thread threadA = new Thread(new ThreadStart(WorkMethod));
threadA.Start();
}
//子进程启动程序
void WorkMethod()
{
this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText("子线程执行完成!");
});
}
//2.使用匿名委托来启动子线程
public Window1()
{
Thread t = new Thread(delegate() {
this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText("子线程执行完成!");
});
});
t.Start();
}
//3.使用匿名委托来启动子线程
public Window1()
{
InitializeComponent();
textBox1.AppendText("主线程执行完成");
new Thread(() =>
{
this.Dispatcher.Invoke(new Action(() =>
{
textBox1.AppendText(Environment.NewLine);
textBox1.AppendText("子线程执行完成");
}));
}).Start();
}
线程同步基础--------------------------------------------------------------------------------
同步要领
锁和线程安全
//将当前线程阻塞指定的时间
Thread.Sleep(TimeSpan.FromSeconds(30));
Interrupt 和 Abort
线程状态
等待句柄
同步环境
使用多线程--------------------------------------------------------------------------------
单元模式和Windows Forms
BackgroundWorker类
ReaderWriterLock类
线程池
异步委托
计时器
public Window1()
{
//计时器 SetTimeInterval
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 0, 100);
t.Tick += new EventHandler(timer_Interval);
t.Start();
}
public Window1()
{
//计时器 SetTimeOut
new System.Threading.Timer(new TimerCallback(timer_Callback), this, 5000, 0);
}
局部储存
高级话题--------------------------------------------------------------------------------
非阻止同步
Wait和Pulse
Suspend和Resume
终止线程
同步线程实例
public Window1()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
this.textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
textBox1.AppendText(Environment.NewLine);
this.textBox1.AppendText("同步线程执行完成!");
});
}
异步线程实例
public Window1()
{
textBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
textBox1.AppendText(Environment.NewLine);
textBox1.AppendText("异步线程执行完成");
}));
}
线程呈现控件实例-可传参数
public Window1()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
t.Tick += new EventHandler(Dispatcher_Timer);
t.Start();
}
private delegate void DispatcherDelegate(string msg);
private void OutPut(string msg)
{
textBox1.Dispatcher.Invoke(new DispatcherDelegate(DelegateMethod), msg);
}
private void DelegateMethod(string msg)
{
textBox1.AppendText(msg);
textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
textBox1.AppendText(Environment.NewLine);
}
void Dispatcher_Timer(object sender, EventArgs e)
{
OutPut("控件呈现:");
}
线程呈现控件实例-使用匿名委托
public Window1()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
t.Tick += new EventHandler(Dispatcher_Timer);
t.Start();
}
void Dispatcher_Timer(object sender, EventArgs e)
{
textBox1.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
{
textBox1.AppendText("控件呈现:");
textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
textBox1.AppendText(Environment.NewLine);
}));
}
线程呈现控件实例-匿名方法并返回参数
public Window1()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer();
t.Interval = new TimeSpan(0, 0, 0, 0, 1000);
t.Tick += new EventHandler(Dispatcher_Timer);
t.Start();
}
void Dispatcher_Timer(object sender, EventArgs e)
{
string strmsg = (string)textBox1.Dispatcher.Invoke(new Func<string>(() =>
{
textBox1.AppendText("控件呈现:");
textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
textBox1.AppendText(Environment.NewLine);
return textBox1.Text;
}));
}
线程:主线程、子线程 同步线程、异步线程 单线程、多线程 System.Threading与System.Windows.Threading的更多相关文章
- 使用C++11 开发一个半同步半异步线程池
摘自:<深入应用C++11>第九章 实际中,主要有两种方法处理大量的并发任务,一种是一个请求由系统产生一个相应的处理请求的线程(一对一) 另外一种是系统预先生成一些用于处理请求的进程,当请 ...
- 使用C++11实现一个半同步半异步线程池
前言 C++11之前我们使用线程需要系统提供API.posix线程库或者使用boost提供的线程库,C++11后就加入了跨平台的线程类std::thread,线程同步相关类std::mutex.std ...
- c++11 实现半同步半异步线程池
感受: 随着深入学习,现代c++给我带来越来越多的惊喜- c++真的变强大了. 半同步半异步线程池: 事实上非常好理解.分为三层 同步层:通过IO复用或者其它多线程多进程等不断的将待处理事件加入到队列 ...
- (原创)C++半同步半异步线程池2
(原创)C++半同步半异步线程池 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 线程池可以高效的处理任务,线程池中开启多个线程,等待同步队列中的任务到来,任务到来多个线程 ...
- Python 中的进程、线程、协程、同步、异步、回调
进程和线程究竟是什么东西?传统网络服务模型是如何工作的?协程和线程的关系和区别有哪些?IO过程在什么时间发生? 一.上下文切换技术 简述 在进一步之前,让我们先回顾一下各种上下文切换技术. 不过首先说 ...
- C++11 半同步半异步线程池的实现
#include <list> #include <mutex> #include <thread> #include <condition_variable ...
- 关于GCD同步组实现多个异步线程的同步执行中的注意点
在App开发中经常会遇到多个线程同时向服务器取数据, 如果每个线程取得数据后都去刷新UI会造成界面的闪烁 也有可能出现部分数据还没有获取完毕造成程序crash 之前在网上看到很多是利用dispatch ...
- 安卓 异步线程更新Ui
异步跟新UI: 1.handler+Thread(runnable):如果handler和Thread都写在了一个Java文件中,就不说了,如果runnable定义在了一个单独的类文件中,可以通过在构 ...
- Erlang运行时中的无锁队列及其在异步线程中的应用
本文首先介绍 Erlang 运行时中需要使用无锁队列的场合,然后介绍无锁队列的基本原理及会遇到的问题,接下来介绍 Erlang 运行时中如何通过“线程进度”机制解决无锁队列的问题,并介绍 Erlang ...
- Spring Boot系列二 Spring @Async异步线程池用法总结
1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...
随机推荐
- JavaScript进阶系列01,函数的声明,函数参数,函数闭包
本篇主要体验JavaScript函数的声明.函数参数以及函数闭包. □ 函数的声明 ※ 声明全局函数 通常这样声明函数: function doSth() { alert("可以在任何时候调 ...
- iOS中使用RegexKitLite来试用正则表达式
转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中 ...
- 美国恐怖故事第七季/全集American Horror Story全迅雷下载
FX电视台已经续订了<美国恐怖故事>第七季,将于2017年开播,第七季终于确定副标题为<邪教 Cult>.剧集的创造者瑞恩·墨菲与布拉德·法尔查克将再度联手.顺便一说,< ...
- Material Designer的低版本兼容实现(十一)—— Switch
5.0中的switch和之前完全不同了,漂亮不漂亮咱们另说,总之4.x上是没有这样的效果了.实现方式有两种,一种是用这个兼容包来做类似的效果,一种是用传统的checkbox来代替.我感觉兼容包的效果是 ...
- Eclipse 离线汉化的方法
本文感谢:http://jingyan.baidu.com/article/e75057f28401a8ebc91a899e.html 首先进入网址:http://www.eclipse.org/ba ...
- HTML5 filesystem: 网址
FileSystem API 使用新的网址机制,(即 filesystem:),可用于填充 src 或 href 属性.例如,如果您要显示某幅图片且拥有相应的 fileEntry,您可以调用 toUR ...
- FastJson、Jackson、Gson进行Java对象转换Json的细节处理
前言 Java对象在转json的时候,如果对象里面有属性值为null的话,那么在json序列化的时候要不要序列出来呢?对比以下json转换方式 一.fastJson 1.fastJson在转换java ...
- State of Serverless
Some quick thoughts from Serverlessconf, Austin in April 2017 I wanted to take a bit of time to writ ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
- python3 CERTIFICATE_VERIFY_FAILED错误 certificate verify failed
在response = request.urlopen(url)打开一个https连接时报如下错误: urllib.error.URLError: <urlopen error [SSL: CE ...