wpf 多线程操作(2)

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 WindowsFormsApplication1
{
   
    public delegate void RunHandler(object a,message mess);
    public delegate void RunHandler1();
    public partial class Form1 : Form
    {
        Thread th;
        Fox fox;
        public Form1()
        {
            InitializeComponent();

            fox = new Fox("小智");

            label1.Text =fox.Name;

            fox.RunEvent += fox_RunEvent1;

            fox.RunEvent1+=fox_RunEvent1;

            

            //button1.Click+=button1_Click;
        }

        private void fox_RunEvent1()
        {
            System.Threading.Thread.Sleep(1000);//延迟一秒
            if (textBox1.InvokeRequired)
            {
                RunHandler1 rh = new RunHandler1(Run);
                textBox1.Invoke(rh);
            }
            else
            {
                textBox1.Text = "我去了,主淫";
            }
            
        }

        

        private void fox_RunEvent1(object a, message mess)
        {
            textBox2.Text = "上吧!皮卡丘";
        }

        

        //private void fox_RunEvent1()
        //{
        //    textBox2.Text = "皮卡丘";
        //}

        //private void fox_RunEvent()
        //{
        //    textBox1.Text = "上吧";
        //}
       
        class Fox
        {
            private string _Name;

            public string Name
            {
                get { return _Name; }
                set { _Name = value; }
            }
            public Fox(string name)
            {
                _Name = name;
            }

            public event RunHandler RunEvent;
            public event RunHandler1 RunEvent1;

            public void BeShooted()
            {
                if (RunEvent!=null)
                {
                    RunEvent(this, new message() {Message="我去了,主淫"});
                }
            }

            public void BeShooted1()
            {
                if (RunEvent1!=null)
                {
                    RunEvent1();
                }
            }
        }
        
        
        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            
            if (i==0)
            {
                fox.BeShooted();
                th = new Thread(new ThreadStart(Run));
                th.Start();
                i = 1;
            }
            else
            {
                textBox1.Text = "";
                textBox2.Text = "";
                i = 0;
            }
            //th.Abort();
            button1.Text = button1.Text.Equals("准备") ? "嗖。。" : "准备";
        }
        private void Run()
        {
            fox.BeShooted1();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    public class message : EventArgs
    {
        public string Message
        {
            get;
            set;
        }
    }
}

 

posted @ 2015-07-02 16:28  王文姿  阅读(339)  评论(0编辑  收藏  举报