Cross-thread calls the Invoke method

Abstract: Cross-thread calls the Invoke method


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

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread NewThread = new Thread(new ThreadStart(NewThreadMethod));   //建立测试用的线程
            NewThread.Start();  //启动测试用的线程
        }

        //原线程,被其它线程调用
        static void Method(int Param)
        {
            int i = Param;
        }

        //声明一个委派,定义参数
        delegate void MyDelegate(int Param);

        //实践委派,指向员线程中被调用的Method
        MyDelegate ShowData = new MyDelegate(Method);

        //测试用的线程,在此调用原线程
        void NewThreadMethod()
        {
            int i = 0;
            while (true)
            {
                this.Invoke(this.ShowData, i);
                Thread.Sleep(2000);
            }
        }
    }
}

Original: Large column  calling Invoke method of cross-thread


Guess you like

Origin www.cnblogs.com/chinatrump/p/11491083.html