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?