我的表格上有几个复选框。
按下按钮我在后端进行一些操作(约3秒钟)。
然后我禁用复选框以阻止用户互动。
但是如果我点击禁用的复选框,它会在启用后做出反应
如何防止这种情况?
答案 0 :(得分:2)
我敢打赌,当您运行资源密集型流程时,您的UI会被冻结。鼠标单击将排队,直到您的进程运行完毕后才会处理。到目前为止,复选框已经重新启用。
我建议使用BackgroundWorker线程来处理繁重的3秒操作。这将使您的UI可以自由处理鼠标点击等操作并解决您的问题。
答案 1 :(得分:0)
您可以使用BackgroundWorker来阻止UI冻结
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress((i * 10));
}
}
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
this.tbProgress.Text = "Canceled!";
}
else if (!(e.Error == null))
{
this.tbProgress.Text = ("Error: " + e.Error.Message);
}
else
{
this.tbProgress.Text = "Done!";
}
}