入门--------------------------------------------------------------------------------
  概述与概念
    一个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. Visual Studio断点调试, 无法监视变量, 提示无法计算表达式

    在使用Visual Studio 2012进行断点调试时,对某个变量添加监视,出现"无法计算表达式"的提示. 解决办法:依次点击菜单栏中的"调试"→" ...

  3. spring集成jpa【为什么有 persistant.xml 文件呢?】

    原文地址: http://www.cnblogs.com/javahuang/archive/2012/12/19/2824633.html spring集成JPA的其中一种方式 JPA和hibern ...

  4. android studio每次启动都要在fetching Android sdk compoment information停好久 怎么解决?

    网上有人给出了方案:1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开.2)在idea.properties文件末尾添加一行: d ...

  5. 技术人生:Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing how to use it.

    Knowing when or where it’s appropriate to use a technique or tool is just as important as knowing ho ...

  6. TextKit简单示例

    TextKit简单示例 效果 源码 https://github.com/YouXianMing/Animations // // TextKitLoadImageController.m // An ...

  7. 无耻之徒(美版)第七季/全集Shameless US迅雷下载

    英文全名Shameless (US),第7季(2016).本季看点:<无耻之徒>(Shameless)第七季.本季故事起始于「一个月之后」,Frank从昏迷中醒来后得知亲人背叛了他,于是向 ...

  8. CSS3 Flex布局整理(二)-容器属性

    一.Flex容器属性介绍 1.flex-flow :水平或垂直方向上的流动方式,包裹处理,其中包括了flex-direction属性和flex-wrap属性. 2.justify-content:定义 ...

  9. jquery fullPage

    FROM : http://www.dowebok.com/77.html 应用: http://txhd.163.com/

  10. Shell操作mysql数据库

    From : http://www.2cto.com/database/201306/220570.html Shell操作mysql数据库   mysql  -hhostname -Pport -u ...