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();
            }

        }

    }
  • 相关阅读:
    BZOJ-3940:Censoring(AC自动机裸题)
    BZOJ-3881:Divljak (AC自动机+DFS序+树链求并+树状数组)
    CodeForces
    CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)
    CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
    CodeForces
    CodeForces
    BZOJ2726:任务安排(DP+斜率优化+二分)
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测
    [SDOI2009]Bill的挑战
  • 原文地址:https://www.cnblogs.com/wubiyu/p/818810.html
Copyright © 2011-2022 走看看