0

I am trying to use Azure Mobile services from my Xamarin Android app. It works. However, I can not get it working without a Timer. In the documentation, I can not find any information about a need for a timer so I want to know how I can use Azure mobile services without a Timer.

Here is my code with a timer. It works. My application doesn't freeze and I can see in the debug output in visual studio that the last line is really executed. Here is my code:

        protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        CurrentPlatform.Init();
        timer = new System.Timers.Timer(1);
        timer.Elapsed += OnTimedEvent;
        timer.Start();
        //GetDataFromAzure();
    }

    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        timer.Stop();
        GetDataFromAzure();
    }

    private void GetDataFromAzure()
    {
        var arguments = new Dictionary<string, string>();
        arguments.Add("input", "TestInput");
        var resultDoe = MobileService.InvokeApiAsync<string>("NameOfController", System.Net.Http.HttpMethod.Get, arguments);
        System.Diagnostics.Debug.WriteLine("Before Wait!!!!!!!!!!!!!");
        resultDoe.Wait();
        System.Diagnostics.Debug.WriteLine("After Wait!!!!!!!!!!!!!");
        var endresult = resultDoe.Result;
        System.Diagnostics.Debug.WriteLine("End Of GetDataFromAzure!!!!!!!!!!!!!! " + endresult);
    }

The code may look strange because of the timer, but it is important to know that this code really works. In the debug output, I can see: "[0:] End Of GetDataFromAzure!!!!!!!!!!!!!! TestOutPut" which shows that the expected result is returned and the method GetDataFromAzure is executed.

Now let's remove the timer. It should still work (but it doesn't). Here is my code without a timer:

        protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.MyButton);
        CurrentPlatform.Init();
        //timer = new System.Timers.Timer(1);
        //timer.Elapsed += OnTimedEvent;
        //timer.Start();
        GetDataFromAzure();
    }

    private void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        timer.Stop();
        GetDataFromAzure();
    }

    private void GetDataFromAzure()
    {
        var arguments = new Dictionary<string, string>();
        arguments.Add("input", "TestInput");
        var resultDoe = MobileService.InvokeApiAsync<string>("NameOfController", System.Net.Http.HttpMethod.Get, arguments);
        System.Diagnostics.Debug.WriteLine("Before Wait!!!!!!!!!!!!!");
        resultDoe.Wait();
        System.Diagnostics.Debug.WriteLine("After Wait!!!!!!!!!!!!!");
        var endresult = resultDoe.Result;
        System.Diagnostics.Debug.WriteLine("End Of GetDataFromAzure!!!!!!!!!!!!!! " + endresult);
    }

The issue becomes clear. resultDoe.Wait() freezes the application when no Timer is used. In the output, I can see "Before Wait!!!!!!!!!!!!!" but I can not see "After Wait!!!!!!!!!!!!!" . Moreover, my application is frozen.

It is clear to me how to be able to use Azure Mobile Services. I get it working. However, it is not clear to me how I can get it working without a timer. I simply fail to write code without a Timer that does the job. How do I make sure I can use Azure Mobile Services in my Android app without a Timer?

1 Answer 1

0

There is absolutely no need to add a timer. The problem is that InvokeApiAsync is a task returning asynchronous method and you are trying to work with it synchronously.

First refactor your GetDataFromAzure method to be something like this:

private async Task GetDataFromAzureAsync()
{
    var arguments = new Dictionary<string, string>();
    arguments.Add("input", "TestInput");
    System.Diagnostics.Debug.WriteLine("Before await!!!!!!!!!!!!!");
    var resultDoe = await MobileService.InvokeApiAsync<string>("NameOfController", System.Net.Http.HttpMethod.Get, arguments);
    System.Diagnostics.Debug.WriteLine("End Of GetDataFromAzure!!!!!!!!!!!!!! " + resultDoe);
}

Then change how you call it, pull out all your timer code and change your OnCreate to be something like:

protected async override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);
    Button button = FindViewById<Button>(Resource.Id.MyButton);
    CurrentPlatform.Init();

    await GetDataFromAzureAsync();
}
4
  • Hmm, So actually this means that this problem requires C# 5 code. Solving the problem with C# 4 is not possible. Am I right? Using Wait() is very common in C# 4 and .NET 4.0 .
    – Daan
    Apr 20, 2015 at 17:22
  • My answer requires .Net 4.5 but it isn't required if for some reason you need interoperability with .Net 4.0 (sorry, I do not see a .Net 4.0 requirement in the question). if 4.0 if you want to get it off the foreground thread you can use Task.Run instead of a timer. If you do, when the call back happens you will need to sync back up to the foreground thread if you want to update anything on the UI. However, if you need to go with 4.0 I'd use the Async Targetting Pack for whatever is still locked to 4.0. stackoverflow.com/questions/9110472/using-async-await-on-net-4
    – Kevin Ford
    Apr 20, 2015 at 19:16
  • Just to be clear (since re-reading my comment isn't clear to me), the Async Targeting Pack allows you to use async/await with .Net versions prior to 4.5 if you need to share code with a project that is using .Net 4.0.
    – Kevin Ford
    Apr 20, 2015 at 19:37
  • Is there anything else I can do to help resolve your question?
    – Kevin Ford
    Apr 22, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.