zoukankan      html  css  js  c++  java
  • c# 多线程基本操作

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;

    namespace winfromDeom
    {
        
    public partial class Form1 : Form
        
    {
            
    public Form1()
            
    {
                InitializeComponent();
            }


            
    private void Form1_Load(object sender, EventArgs e)
            
    {
                button2.Enabled 
    = false;
            }


            
    //声名一个线程
            public Thread t;

            
    public delegate void upUI();

            
    private void button1_Click(object sender, EventArgs e)
            
    {
                progressBar1.Value 
    = 0;
                button1.Enabled 
    = false;
                button2.Enabled 
    = true;
                
    //实列化一个线程
                t = new Thread(new ThreadStart(doThread));
                
    //设置是否是后台执行的线程
                t.IsBackground = true;
                
    //开始执行这个线程
                t.Start();

            }


            
    private void doThread()
            
    {
                
    //更新UI委托
                upUI upui = new upUI(upDataUi);
                
                
    try
                
    {
                    
    while (true)
                    
    {
                        Thread.Sleep(
    0);
                        
    //开始更新UI
                        this.Invoke(upui);
                    }

                }

                
    finally
                
    {
                    Thread.Sleep(
    5000);
                }

            }


            
    private void button2_Click(object sender, EventArgs e)
            
    {
                
    //设置为终止状态
                t.Abort();
                
    //阻塞这个线程
                t.Join();
                button1.Enabled 
    = true;
                button2.Enabled 
    = false;
            }

            
    /// <summary>
            
    /// 执行UI更新的方法
            
    /// </summary>

            private void upDataUi()
            
    {
                
    if (progressBar1.Value != 100000)
                
    {
                    progressBar1.Value 
    = progressBar1.Value + 10;
                    label1.Text 
    = progressBar1.Value.ToString();
                }

                
    else
                
    {
                    t.Abort();
                    t.Join();
                    button1.Enabled 
    = true;
                    button2.Enabled 
    = false;
                }


            }

         
            
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            
    {
                t.Abort();
                t.Join();
            }

        }

    }
  • 相关阅读:
    Linux 技巧
    设计模式大集合
    [Java] Design Pattern:Code Shape
    [Java] 设计模式: Code Shape
    BookNote: Refactoring
    读书笔记: 博弈论导论
    读书笔记: 博弈论导论
    读书笔记: 博弈论导论
    读书笔记: 博弈论导论
    读书笔记: 博弈论导论
  • 原文地址:https://www.cnblogs.com/wubiyu/p/818810.html
Copyright © 2011-2022 走看看