我有一個按鈕,點擊事件發送並從服務器獲取數據並將其顯示在網格上。如何知道異步方法是否正在運行
的代碼如下圖所示:
private void btnSearch_Click(object sender, EventArgs e)
{
// Here I should do something in order to know if the async ProcessSearch method is busy..
// if not busy then I will execute it if not then I will return.
// shows loading animation
ShowPleaseWait(Translate("Searching data. Please wait..."));
ProcessSearch();
}
private async void ProcessSearch()
{
Data.SeekWCF seekWcf = new Data.SeekWCF();
_ds = await seekWcf.SearchInvoiceAdminAsync(new Guid(cboEmployer.Value.ToString()), new Guid(cboGroup.Value.ToString()), txtSearchInvoiceNumber.Text, chkSearchLike.Checked, txtSearchFolio.Text, Convert.ToInt32(txtYear.Value));
seekWcf.Dispose();
if (_ds != null)
{
SetupInvoiceGrid();
}
// hides the loading animation
HidePleaseWait();
}
我怎麼能知道,如果異步方法ProcessSearch忙或運行,所以我可以阻止用戶再次點擊該按鈕時,再次執行方法。
BTW,你應該只在使用'異步void'的頂層。也就是說,'ProcessSearch()'應該是一個'async Task'方法,'btnSearch_Click'應該是一個'async void'方法,它等待'ProcessSearch()'的結果。 – svick