入门--------------------------------------------------------------------------------
  概述与概念
    一个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的更多相关文章

  1. 使用C++11 开发一个半同步半异步线程池

    摘自:<深入应用C++11>第九章 实际中,主要有两种方法处理大量的并发任务,一种是一个请求由系统产生一个相应的处理请求的线程(一对一) 另外一种是系统预先生成一些用于处理请求的进程,当请 ...

  2. 使用C++11实现一个半同步半异步线程池

    前言 C++11之前我们使用线程需要系统提供API.posix线程库或者使用boost提供的线程库,C++11后就加入了跨平台的线程类std::thread,线程同步相关类std::mutex.std ...

  3. c++11 实现半同步半异步线程池

    感受: 随着深入学习,现代c++给我带来越来越多的惊喜- c++真的变强大了. 半同步半异步线程池: 事实上非常好理解.分为三层 同步层:通过IO复用或者其它多线程多进程等不断的将待处理事件加入到队列 ...

  4. (原创)C++半同步半异步线程池2

    (原创)C++半同步半异步线程池 c++11 boost技术交流群:296561497,欢迎大家来交流技术. 线程池可以高效的处理任务,线程池中开启多个线程,等待同步队列中的任务到来,任务到来多个线程 ...

  5. Python 中的进程、线程、协程、同步、异步、回调

    进程和线程究竟是什么东西?传统网络服务模型是如何工作的?协程和线程的关系和区别有哪些?IO过程在什么时间发生? 一.上下文切换技术 简述 在进一步之前,让我们先回顾一下各种上下文切换技术. 不过首先说 ...

  6. C++11 半同步半异步线程池的实现

    #include <list> #include <mutex> #include <thread> #include <condition_variable ...

  7. 关于GCD同步组实现多个异步线程的同步执行中的注意点

    在App开发中经常会遇到多个线程同时向服务器取数据, 如果每个线程取得数据后都去刷新UI会造成界面的闪烁 也有可能出现部分数据还没有获取完毕造成程序crash 之前在网上看到很多是利用dispatch ...

  8. 安卓 异步线程更新Ui

    异步跟新UI: 1.handler+Thread(runnable):如果handler和Thread都写在了一个Java文件中,就不说了,如果runnable定义在了一个单独的类文件中,可以通过在构 ...

  9. Erlang运行时中的无锁队列及其在异步线程中的应用

    本文首先介绍 Erlang 运行时中需要使用无锁队列的场合,然后介绍无锁队列的基本原理及会遇到的问题,接下来介绍 Erlang 运行时中如何通过“线程进度”机制解决无锁队列的问题,并介绍 Erlang ...

  10. Spring Boot系列二 Spring @Async异步线程池用法总结

    1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...

随机推荐

  1. VS2015开发环境的安装和配置 2016-07-03更新

    创建日期:2016-07-03 一.简介 为了避免网上乱七八糟的过时介绍,避免误导初学者,这次把至2016年6月底C#开发环境各种版本的更新和安装过程重新整理一下贡献出来.目的是为了让对C#感兴趣的初 ...

  2. Xamarin.Android,Xamarin.iOS, Linking

    Xamarin.Android applications use a linker in order to reduce the size of the application. The linker ...

  3. Flex+blazeds实现与mySQL数据库的连接(已成功实现此文的例子)

    http://bdk82924.iteye.com/blog/1067285 几个下载地址 blazeds_turnkey_3-0-0-544.zip 下载地址:http://download.mac ...

  4. 在ASP.NET MVC下有关上传图片脏数据的解决方案

    在"在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片"中,已经实现了在客户端和服务端限制图片大小和格式,以及在服务端裁剪图片.但还 ...

  5. C#编程(二十一)----------扩展方法

    C#中的扩展方法 有许多扩展类的方式.如果有类的源代码,继承就是给类添加功能的好方法.但是如果没有源代码,怎么办?吃屎可以使用扩展方法,它允许改变一个类,但不需要该类的源代码.扩展方法是静态方法,它是 ...

  6. ormlite 在android中 排序 条件查询

    ormlite 在android中 排序 条件查询 all = dao.queryBuilder().orderBy("Id", true).where().eq("Ty ...

  7. maven 阿里仓库

    <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3. ...

  8. [A类会议] 国内论文检索

    https://www.cn-ki.net/ http://www.koovin.com

  9. [转]linux最新分区方案

    FROM : http://www.cnblogs.com/chenlulouis/archive/2009/08/27/1554983.html 我的服务器是500G.最重要的是/var分区一定要大 ...

  10. 关于XSHM(Cross-Site History Manipulation)

    http://blog.chinaunix.net/uid-27070210-id-3255407.html 乍一看,好像和以前 css history hack 差不多,其实原理还是不一样的.浏览器 ...