如何在超时后关闭活动

时间:2015-12-24 05:52:40

标签: c# android xamarin mvvmcross

我在Android应用程序中有登录屏幕,有超过45秒的超时时间,它还会显示“超时”的弹出窗口'但是在点击弹出应用程序崩溃之后,该活动没有关闭或完成那里

public HttpClient Client
        {
            get
            {
                if (_client != null) return _client;


                var httpClient = new HttpClient(new NativeMessageHandler {UseCookies = false});
                httpClient.BaseAddress = new Uri(ApplicationSettings.BaseServiceUrl);
                httpClient.Timeout = TimeSpan.FromMilliseconds(45000);
                return _client = httpClient;

            }
        }

        public HttpClient ClientPreAuthenticated
        {
            get
            {
                if (_client != null) return _client;

                var httpClient = new HttpClient(new NativeMessageHandler { UseCookies = false, PreAuthenticate = true });
                httpClient.BaseAddress = new Uri(ApplicationSettings.BaseServiceUrl);
                httpClient.Timeout = TimeSpan.FromMilliseconds(45000);
                return _client = httpClient;
            }
        }

建议我关闭此活动的任何代码

2 个答案:

答案 0 :(得分:1)

Task.Factory.StartNew(async()=> {await Task.Delay(45000); Finish();});

答案 1 :(得分:0)

在您的调用者(btnLoginClicked)或其他任何内容中,您需要处理从httpclient代码超时触发的Timeout异常或事件..然后只需在catch子句中调用Finish()即可。如果不这样做似乎知道如何捕获从你的HttpClient提出的超时,也许可以看出来:How can I tell when HttpClient has timed out?