3

I have been using in a WinForms C# application BackgroundWorkers to do any WCF service data calls like below:

private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            switch (_workerType)
            {
                case "search":


                    Data.SeekWCF seekWcf = new Data.SeekWCF();
                    _ds = seekWcf.SearchInvoiceAdmin(new Guid(cboEmployer.Value.ToString()), new Guid(cboGroup.Value.ToString()), txtSearchInvoiceNumber.Text, chkSearchLike.Checked, txtSearchFolio.Text, Convert.ToInt32(txtYear.Value));
                    seekWcf.Dispose();

                    break;

                case "update":

                    Data.AccountingWCF accWcf = new Data.AccountingWCF();
                    _returnVal = accWcf.UpdateInvoiceAdmin(_ds);
                    accWcf.Dispose();

                    break;
            }
        }

In order to call the background worker I do for example:

private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!_worker.IsBusy)
                {

                    ShowPleaseWait(Translate("Searching data. Please wait..."));
                    _workerType = "search";
                    _worker.RunWorkerAsync();
                }
        }

Now, I would like to start migrating to Task (async / await) C# 5.0, so what I did in this example was:

private async void ProcessSearch()
        {

            Data.SeekWCF seekWcf = new Data.SeekWCF();
            _ds = seekWcf.SearchInvoiceAdmin(new Guid(cboEmployer.Value.ToString()), new Guid(cboGroup.Value.ToString()), txtSearchInvoiceNumber.Text, chkSearchLike.Checked, txtSearchFolio.Text, Convert.ToInt32(txtYear.Value));
            seekWcf.Dispose();
        }

But here I get a message saying that "this async method lacks 'await' operators and will run synchronously".

Any clue on how is the best practice and the right way to do what I want to accomplish?

svick
  • 236,525
  • 50
  • 385
  • 514
VAAA
  • 14,531
  • 28
  • 130
  • 253

1 Answers1

1

Ideally, your WCF service wrapper should have Async versions of its methods, i.e. SearchInvoiceAdminAsync. Then you'd await for it:

        private async Task ProcessSearchAsync()
        {

            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();
        }

EDITED: A different matter is that eventually you'd need to call your async method from a regular method, e.g, upon a button click, and perhaps do something when the task is done. Here is a very simplistic scenario:

// UI Thread

Task _pendingTask = null;

void button_click()
{
    if ( _pendingTask != null)
    {
        MessageBox.Show("Still working!");
    }
    else
    {
        _pendingTask = ProcessSearchAsync();


        _pendingTask.ContinueWith((t) =>
        {
            MessageBox.Show("Task is done!");
            // check _pendingTask.IsFaulted here
            _pendingTask = null;
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }
}

EDITED: Note, initially I forgot to specify TaskScheduler when calling ContinueWith. That might have resulted in continuationAction being called on a pool thread.

avo
  • 10,101
  • 13
  • 53
  • 81
  • How can I convert my WCF Method to an Async method? – VAAA Aug 17 '13 at 15:56
  • Just regenerate the C# wrapper for it, here is [how](http://msdn.microsoft.com/en-us/library/ms730059.aspx). – avo Aug 17 '13 at 15:59
  • What I did was to right click on the service and "Configure service reference..." and there I check the option "Allow generation of async operations".. and compile it again and I dont see any Async method added.. any clue=? – VAAA Aug 17 '13 at 16:03
  • Below "Allow generation of async operations" there're also two radio buttons, "Generate Task-based operations" and "Generate async operations". Select the latter. – avo Aug 17 '13 at 16:11
  • Correction: "Generate Task-based operations" (the first option). But that's what you had there by default, right? Maybe you should remove the reference and add it again. – avo Aug 17 '13 at 16:23
  • maybe.. let me try remove the service and add it again – VAAA Aug 17 '13 at 16:25
  • Great!! Worked!! Thanks a lot :) – VAAA Aug 17 '13 at 16:26
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35660/discussion-between-vaaa-and-avo) – VAAA Aug 17 '13 at 16:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35693/discussion-between-vaaa-and-avo) – VAAA Aug 18 '13 at 13:53