匿名代理的使用方式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace TestDelegate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        private void LoopWriteOutput()
        {
            while (true)
            {
                WriteOutput("GO GO GO");
                Thread.Sleep(500);
            }
        }

        private void WriteOutput(string text)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate()
                {
                    WriteOutput(text);
                }));
            }
            else
            {
                textBox1.AppendText(text + Environment.NewLine);
                textBox1.Select(textBox1.Text.Length - 1, 0);
                textBox1.ScrollToCaret();
            }
        }
    }
}
原文地址:https://www.cnblogs.com/lgyup/p/7998372.html