zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    微信小程序之某个节点距离顶部和底部的距离 createSelectorQuery
    js正则手机号 验证
    算法将一个对象中的某一个key值变为true,其他值都为false
    更改上传框的大小
    Educational Codeforces Round 85 (Div. 2)
    Codeforces Round #632 (Div. 2)
    AtCoder Beginner Contest 161
    Codeforces Round #631 (Div. 2)
    Codeforces Round #630 (Div. 2)
    Codeforces Round #629 (Div. 3)
  • 原文地址:https://www.cnblogs.com/w-wz/p/4616420.html
Copyright © 2011-2022 走看看