the_tr00per
the_tr00per

Reputation: 439

Xamarin.ios run a WCF Service function using await

I've followed the following example which works well.

https://developer.xamarin.com/guides/cross-platform/application_fundamentals/web_services/walkthrough_working_with_WCF/

The only issue is, the example uses button clicks to load data. Now i have two separate calls different functions, and i need one to wait for the other, for example:

So when i call function2 for example, i want to wait for function1 first.

_client.function1Async();
_client.function2Async();

I could put the function2 call inside the function1Completed handler, but i was looking to use async wait with it. When i use async task, i get an error saying cannot await a void. But the web service async function in the example is a void

If i had 6 calls that i wanted to run, it would become very messy.

void _client_function1Completed(object sender,UpdateOrdersByWardCompletedEventArgs e
{
}

void _client_function2Completed(object sender,UpdateOrdersByWardCompletedEventArgs e
{
}

Hope this makes sense.

Upvotes: 0

Views: 226

Answers (2)

megaKertz
megaKertz

Reputation: 484

You could Task.ContinueWith ContinueWith creates a continuation that executes asynchronously when task 1 completes.

var task1 = Task.Factory.StartNew( () => { func1(); } );

Task task2 = task1 .ContinueWith( (i) => { func2(); } );

Upvotes: 2

Fernando
Fernando

Reputation: 535

Not sure if I understood well what you are trying to achieve, but I guess you can try something like this:

Given some lengthy (and blocking) methods:

public void func1()
{
    Console.WriteLine("func1");
    System.Threading.Thread.Sleep(5000);
}

public void func2()
{
    Console.WriteLine("func2");
    System.Threading.Thread.Sleep(5000);
}

You could add the ability to run the lengthy stuff asynchronously by doing this:

public async Task function1Async()
{
   await Task.Run(() => {
      func1();
   });
}

public async Task function1Async()
{
   await Task.Run(() => {
      func2();
   });
}

Now you can choose to run 'func1' and 'func2' either asynchronously or synchronously, for example:

function1Async().Wait();    // will block
function2Async();           // will run asynchronously

So, for your particular case, given that you already have the two async methods, I guess that all you need to do is to call them as shown above.

Upvotes: 1

Related Questions