I have the following Async method:
private async void ProcessSearch() { // get catalogs on first search if (_invoiceTypes == null && _invoiceAccounts == null) { var confWcf = new Data.ConfigurationWCF(); _invoiceTypes = await confWcf.GetInvoiceTypesAsync(MainForm.State.Entity); _invoiceAccounts = await confWcf.GetInvoiceAccountsAsync(MainForm.State.Entity); confWcf.Dispose(); } var seekWcf = new DataSeekWCF(); _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(); } }
I do not want to run SetupInvoiceGrid until _invoiceTypes, _invoiceAccounts, and _ds are complete.
Any clue? Am I doing it right? Should I use Task instead of waiting?
I came up with this code that seems to work and looks good to me, but really don't know if this is correct:
private void btnSearch_Click(object sender, EventArgs e) { lock (lockObj) { if (_isBusy) return; else _isBusy = true; } ShowPleaseWait(Translate("Searching data. Please wait...")); if (_invoiceTypes == null && _invoiceAccounts == null) { var t = GetCatalogs(); t.ContinueWith(t2 => { if (t.IsCompleted) ProcessSearch(); }); } else { ProcessSearch(); } } private async Task GetCatalogs() { // get catalogs on first search Data.ConfigurationWCF confWcf = new Data.ConfigurationWCF(); var task1 = confWcf.GetInvoiceTypesAsync(1); var task2 = confWcf.GetInvoiceAccountsAsync(1); confWcf.Dispose(); await Task.WhenAll(task1, task2); _invoiceTypes = task1.Result; _invoiceAccounts = task2.Result; if (_invoiceTypes != null) { cboInvoiceType.DataSource = _invoiceTypes.Tables["invoice_types"]; cboInvoiceType.DisplayMember = "description"; cboInvoiceType.ValueMember = "code"; } } private async void ProcessSearch() { var 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(); } HidePleaseWait(); } cboEmployer.Value.ToString ()), new Guid (cboGroup.Value.ToString ()), txtSearchInvoiceNumber.Text, chkSearchLike.Checked, txtSearchFolio.Text, Convert.ToInt32 (txtYear.Value private void btnSearch_Click(object sender, EventArgs e) { lock (lockObj) { if (_isBusy) return; else _isBusy = true; } ShowPleaseWait(Translate("Searching data. Please wait...")); if (_invoiceTypes == null && _invoiceAccounts == null) { var t = GetCatalogs(); t.ContinueWith(t2 => { if (t.IsCompleted) ProcessSearch(); }); } else { ProcessSearch(); } } private async Task GetCatalogs() { // get catalogs on first search Data.ConfigurationWCF confWcf = new Data.ConfigurationWCF(); var task1 = confWcf.GetInvoiceTypesAsync(1); var task2 = confWcf.GetInvoiceAccountsAsync(1); confWcf.Dispose(); await Task.WhenAll(task1, task2); _invoiceTypes = task1.Result; _invoiceAccounts = task2.Result; if (_invoiceTypes != null) { cboInvoiceType.DataSource = _invoiceTypes.Tables["invoice_types"]; cboInvoiceType.DisplayMember = "description"; cboInvoiceType.ValueMember = "code"; } } private async void ProcessSearch() { var 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(); } HidePleaseWait(); }
multithreading c # task-parallel-library async-await
VAAA Aug 17 '13 at 17:03 2013-08-17 17:03
source share