WinForm Control.Invoke&Control.BeginInvoke异步操作控件实例

参考:http://www.cnblogs.com/yuyijq/archive/2010/01/11/1643802.html

效果图: 

实例(实验)代码:

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

namespace WinFrm1
{
    public partial class frm_async : Form
    {
        private string QueryDataBase()
        {
            Thread.Sleep(2 * 1000);
            return "QueryDataBase OK";
        }
        public frm_async()
        {
            InitializeComponent();
        }

        private void SetText(string ret)
        {
            this.lblResult.Text += "
"+ ret;
        }

        private void btnLongTime_Click(object sender, EventArgs e)
        {
            Test();
        }

        private void frm_async_Load(object sender, EventArgs e)
        {
            Test();
        }

        private void Test()
        {
            Func<string> func = () => QueryDataBase();

            func.BeginInvoke((result) =>
            {
                string ret = func.EndInvoke(result);
                this.BeginInvoke(new Action<string>(SetText), ret);
            }, null);
        }

    }
}
原文地址:https://www.cnblogs.com/guo2001china/p/5549486.html