4

I have got the code that using WCF, requests data from the server.

For Example:

    public static Company LoadCompanyInfo(Guid session)
    {
        var client = new QualerServiceClient("QualerService");
        return client.GetCompanyInfo(session);
    }

I need to make my wpf application to run these code asynchronously.

I try:

    public static Company LoadCompanyInfoAsync(Guid session)
    {
        var client = new QualerServiceClient("QualerService");
        client.BeginGetCompanyInfo(session, new AsyncCallback(EndLoadCompanyInfoAsync), client);

        // How to Get Data from AsyncCallback function?
        return null;
    }

    private static void EndLoadCompanyInfoAsync(IAsyncResult r)
    {
        var client = r.AsyncState as QualerServiceClient;
        var result = client.EndGetCompanyInfo(r);
        // how to return the result value ??
    }

But I don't know how to return data from callback function.

I have got methods:

  • BeginGetCompanyInfo and EndGetCompanyInfo

  • GetCompanyInfoAsync

and event:

  • GetCompanyInfoCompleted.

Quastions:

  1. How can I get the data from the callback method?

  2. What is the difference between GetCompanyInfoAsync and Begin\End?

  3. Best Practices: How can execute a method asynchronously, so that the GUI of my WPF App is not freezes?

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Serhii Kyslyi
  • 1,745
  • 24
  • 43
  • We don't know where QualerServiceClient comes from... – franssu Aug 23 '12 at 15:42
  • does it matter? I want to know, how I can get data from callback and some other quastions in list. – Serhii Kyslyi Aug 23 '12 at 16:11
  • Where it matters a little because you are calling one of its functions. If you want to learn about AsycnCallbacks : http://stackoverflow.com/questions/1047662/what-is-asynccallback Best practice to execute code asynchronously in WPF is usually to use a background worker. – franssu Aug 23 '12 at 16:15
  • Generally, questions should be limited to a single, contained question that has one or more single, contained answer to one and the same issue. =) – J. Steen Aug 23 '12 at 18:08

3 Answers3

3

I'm assuming that you're using VS2012.

First, if your target is .NET 4.0, then install (via NuGet) the Async Targeting pack. If your target is .NET 4.5, you don't have to do anything special.

Next, re-create your client proxy. The existing Begin/End/Async endpoints will be replaced by a single Task<Company> GetCompanyInfoAsync(Guid).

Now you can use it like this:

public static Task<Company> LoadCompanyInfoAsync(Guid session)
{
  var client = new QualerServiceClient("QualerService");
  return client.GetCompanyInfoAsync(session);
}

public async void ButtonClickOrWhatever(...)
{
  var company = await LoadCompanyInfoAsync(mySession);
  // Update UI with company info.
}

The old Begin/End method pair was using the Asynchronous Programming Model (APM). The old Async/event pair was using the Event-based Asynchronous Programming model (EAP). The new Async method is using the Task-based Asynchronous Programming model (TAP). I have more information on async WCF on my blog.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

You could use the async CTP for .net 4, which will become part of .net in 5.

It adds two new keywords into C# the async and await keywords.
You mark a method or lamda with the async keyword and return a Task or Task<T>

when you call your method you would call it like so

var result = await LoadCompanyInfo(sessionId);

Async CTP

    public static async Task<Company> LoadCompanyInfo(Guid session)
    {
      Company result = default(Company);
      await TaskEx.Run(()=>{
      var client = new QualerServiceClient("QualerService");
      result = client.GetCompanyInfo(session);
      });

     return result;
    }
ywm
  • 1,107
  • 10
  • 14
0

Use TPL:

Task t = TaskFactory.FromAsync(
             beginMethod: BeginGetCompanyInfo,
             endMethod: EndGetCompanyInfo,
             arg1: session,
             state: null);
t.ContinueWith(result =>
    {
        // handle result
    });
abatishchev
  • 98,240
  • 88
  • 296
  • 433