现在的位置: 首页 > 综合 > 正文

c# 事件用法2

2013年05月11日 ⁄ 综合 ⁄ 共 777字 ⁄ 字号 评论关闭

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 WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(test_show);
            thread.IsBackground = true;
            thread.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(DownloadInWhData);
            thread.IsBackground = true;
            thread.Start();
        }
        void test_show()
        {

            while (true)
            {
                Console.WriteLine("runing test_show");
                Thread.Sleep(4000);

            }

        }

        public void DownloadInWhData()
        {
            this.Invoke((EventHandler)(delegate
              {
                  while (true)
                  {
                      Console.WriteLine("runing delegate");
                      Thread.Sleep(6000);

                  }
              }));
        }
    }

按钮2执行的线程会同步等待这个线程运行完,按钮1不会。

抱歉!评论已关闭.