我希望在按钮点击事件中运行进程时禁用textbox
。
我注意到textbox
在事件发生后被禁用,而radiobuttons
中的groupbox
立即被禁用。
该按钮应该在开始时被禁用,并且根据某些方法的返回值,它应该恢复启用或保持禁用状态。
这是我目前的代码:
private async void BtnConfirmClick(object sender, EventArgs e)
{
try
{
textbox.Enabled = false;
groupbox.Enabled = false;
await somemethod()
...
}
}
在按钮事件中更改文本时遇到同样的问题。
答案 0 :(得分:0)
private async void BtnConfirm_Click(object sender, EventArgs e)
{
try
{
// you dont want user to click twice
BtnConfirm.Enabled = false;
// ConfigureAwait(false) configures the task so it doesn't need to block caller thread
await Somemethod().ConfigureAwait(false);
}
finally
{
// BeginInvoke prevents thread access exceptions
BeginInvoke((Action)delegate
{
BtnConfirm.Enabled = true;
});
}
}
private async Task Somemethod()
{
await Task.Delay(TimeSpan.FromSeconds(5));
}
就是这样。确保在ConfigureAwait(false)之后在BeginInvoke中进行任何UI操作,因为任务可能最终会在另一个线程上结束。 希望它有所帮助。