简体   繁体   English

如何确保无需计时器即可在Android应用程序中使用Azure移动服务?

[英]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.

相关问题 如何在我的WP 8.1应用程序中使用Azure移动服务和SQL Azure StoredProc功能进行用户注册页面 - How can I use Azure Mobile Services and SQL Azure StoredProc function in my WP 8.1 App for user registration page 如何在Azure移动应用服务客户端中实现AOP? - How do I implement AOP in an Azure Mobile App Services client? 我可以在Windows Phone 8.1应用程序的Azure移动服务中使用自己的数据库吗? - Can i use my own database in Azure mobile services for Windows Phone 8.1 apps? 我如何确保 Unity 可以找到我的游戏 object 而不会返回 Null? 问题是在调用之前初始化 object - How do I make sure that Unity can find my game object without it returning as Null? Problem is with initializing the object before calling it 在Azure移动应用程序服务客户端中,如何设置userId以使loginAsync有效? - In an Azure Mobile App Services client, how do I set the userId so loginAsync works? 如何从Microsoft Azure Mobile Services中的表中读取数据并将其放入我的应用程序中? - How can I read data from a table in Microsoft Azure Mobile Services and put it in my application? 如何从Azure移动服务返回异常 - How do I return an exception from Azure Mobile Services 我如何确保分配给我的应用的UWP协议是唯一的,如果不唯一,该怎么办? - How can I make sure that the UWP protocol I assigned to my app is unique and what will happen if it's not? 我在哪里可以找到我在 azure 应用服务中的日志? - Where do I find my logs in azure app services? 如何使用RegEx确保在TextBox&gt;中写入了有效的电子邮件? - How can I use RegEx to make sure a valid email is written in my TextBox>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM