首页 技术 正文
技术 2022年11月7日
0 收藏 708 点赞 998 浏览 4769 个字

入门——————————————————————————–
  概述与概念
    一个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;
        }));
    }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838