[英]How do I make sure I can use Azure Mobile Services in my Android app without a Timer?
I am trying to use Azure Mobile services from my Xamarin Android app. 我正在尝试使用Xamarin Android应用程序中的Azure移动服务。 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.
在文档中,我找不到有关计时器需求的任何信息,因此我想知道如何在没有计时器的情况下使用Azure移动服务。
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.
我的应用程序没有冻结,我可以在Visual Studio的调试输出中看到最后一行确实已执行。 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.
在调试输出中,我可以看到:“ [0:] GetDataFromAzure的结尾!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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.
没有使用Timer时,resultDoe.Wait()将冻结应用程序。 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. 我很清楚如何使用Azure移动服务。 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?
如何确保无需计时器即可在Android应用程序中使用Azure移动服务?
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.
问题在于InvokeApiAsync是一个返回异步方法的任务,您正在尝试同步使用它。
First refactor your GetDataFromAzure method to be something like this: 首先,将您的GetDataFromAzure方法重构为如下形式:
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: 然后更改其调用方式,取出所有计时器代码,并将OnCreate更改为类似以下内容:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.