多线程

来源:互联网 发布:按键精灵过去网页数据 编辑:程序博客网 时间:2024/05/17 19:14
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace _2222222ok
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 使用线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个任务t
            Task t = new Task(() =>
            {
                go();
            });


            t.Start();//启动任务t


            //如果任务成功完成后执行下面的内容(这个程序是死循环,没有结束)
            t.ContinueWith((task) =>
            {
                int a = 1;
                a = 2;
            });
        }


        /// <summary>
        /// 未使用线程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            go();
        }


        /// <summary>
        /// 执行的方法
        /// </summary>
        private void go()
        {
            while (true)
            {
                //你可以在这里加一个暂停1秒后在执行之类的代码 这样的话一直点“开始死循环”按钮的效果更好
                textBox1.AppendText(DateTime.Now + Environment.NewLine);
            }
        }


        
    }
}
0 0
原创粉丝点击