首页 > 编程语言 > 详细

Chapter 9. 多线程

时间:2016-07-07 22:13:38      阅读:218      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 多线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Thread th;

        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个线程去执行这个方法
            th = new Thread(Test);

            //将线程设置为后台线程
            th.IsBackground = true;

            //标记这个线程准备就绪,可以随时被执行,
            //但具体什么时候执行,由cpu决定
            th.Start();
        }

        /// <summary>
        /// 显示10000以内的数
        /// </summary>
        private void Test()
        {
            for (int i = 0; i < 10000; i++)
            {
                textBox1.Text = i.ToString();

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //取消跨线程的访问
            Control.CheckForIllegalCrossThreadCalls = false;

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //当点击关闭窗体的时候,判断新线程是否为null
            if (th != null)
            {
                //结束这个线程
                th.Abort();
            }
        }
    }
}

技术分享技术分享技术分享

 

Chapter 9. 多线程

原文:http://www.cnblogs.com/xiao55/p/5651574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!