首页 > 编程语言 > 详细

线程等待练习

时间:2020-05-04 16:21:29      阅读:57      评论: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.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        EventWaitHandle _waitHandle = new AutoResetEvent (false);

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("主0");
            //Thread t = new Thread(test);
            //t.Start();

            //Console.WriteLine("主1");
            //t.Join();
            //Console.WriteLine("主2");


            //Thread t1 = new Thread(test);
            //t1.Start();
            //Console.WriteLine("主1");
            //_waitHandle.WaitOne();
            //Console.WriteLine("主2");


            Task task = new Task(tasktest);
            task.Start();

            Console.WriteLine("主1");
            task.Wait();
            Console.WriteLine("主2");

        }


        private void test()
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(1000);
            }
            _waitHandle.Set();
            Console.WriteLine("test" + Thread.CurrentThread.ManagedThreadId);
        }

        private void tasktest()
        {
            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(1000);
            }
           
            Console.WriteLine("test" + Thread.CurrentThread.ManagedThreadId);
        }
    }
}
View Code

 

线程等待练习

原文:https://www.cnblogs.com/anyihen/p/12826676.html

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