using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
m_DebugActionDic = new Dictionary<object, Func<bool>>();
}
private Dictionary<object, Func<bool>> m_DebugActionDic;
private string m_ErrorMessage;
private void ResultCallBack(IAsyncResult res)
{
Control ctrl = res.AsyncState as Control;
ctrl.Invoke(new Action(() => ctrl.Enabled = true));
if (!m_DebugActionDic[res.AsyncState].EndInvoke(res))
{
MessageBox.Show(m_ErrorMessage);
}
m_DebugActionDic.Remove(res.AsyncState);
}
private bool Button1Action()
{
try
{
int floor = int.Parse(textBox1.Text);
Thread.Sleep(3000);
return true;
}
catch (Exception ex)
{
m_ErrorMessage = ex.Message;
return false;
}
}
private bool Button2Action()
{
try
{
int floor = int.Parse(textBox2.Text);
Thread.Sleep(3000);
return true;
}
catch (Exception ex)
{
m_ErrorMessage = ex.Message;
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
Control ctrl = sender as Control;
ctrl.Enabled = false;
m_DebugActionDic.Add(sender, new Func<bool>(Button1Action));
m_DebugActionDic[sender].BeginInvoke(ResultCallBack, sender);
}
private void button2_Click(object sender, EventArgs e) // Button 时间可以都关联一个就可以了
{
Control ctrl = sender as Control;
ctrl.Enabled = false;
m_DebugActionDic.Add(sender, new Func<bool>(Button2Action));
m_DebugActionDic[sender].BeginInvoke(ResultCallBack, sender);
}
}
}
标签:Control,sender,ctrl,C#,res,private,AsyncCallback,DebugActionDic,Winform
From: https://www.cnblogs.com/huvjie/p/17397890.html