• 利用委托的BeginInvoke防止界面假死的实例


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

            private delegate int Mydelegate();
            private int Method()
            {
                Thread.Sleep(10000);
                Random rad = new Random();
                int i = rad.Next(1000);
                return i;
            }

            private void MyMethodComplete(IAsyncResult asyncresult)
            {
                if (asyncresult == null)
                    return;
                textBox1.Text = (asyncresult.AsyncState as Mydelegate).EndInvoke(asyncresult).ToString() ;
                button1.Enabled = true;
             
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Mydelegate my = Method;
                button1.Enabled = false;
                IAsyncResult result= my.BeginInvoke(MyMethodComplete, my);
                if (!result.IsCompleted)
                {
               
               
                }
               
            }
        }
    }

  • 相关阅读:
    解决页面报错: GEThttp://localhost:8080/favicon.ico 404 (Not Found)
    vs2019快捷键整理
    js保存图片至本地
    PHP输出方式的区别
    js运算精度问题
    Hadoop相关问题
    2015年10月5日 12:49:07
    Hive数据倾斜
    Hadoop错误日志
    使用maven开发MR
  • 原文地址:https://www.cnblogs.com/lijinchang/p/2035464.html
Copyright © 2020-2023  润新知