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

效果图: 

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

实例(实验)代码:

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 += "\r\n"+ 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);
        }

    }
}

 

相关文章:

  • 2021-05-24
  • 2022-12-23
  • 2021-09-17
  • 2022-12-23
  • 2021-10-03
  • 2022-01-31
猜你喜欢
  • 2021-07-17
  • 2021-06-02
  • 2021-09-07
  • 2022-12-23
  • 2021-12-17
相关资源
相似解决方案