匿名代理的使用方式

时间:2017-12-07 12:43:04   收藏:0   阅读:419
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();
            }
        }
    }
}

 

原文:http://www.cnblogs.com/lgyup/p/7998372.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!