我正在开发 Windows Phone 8.1 购物应用程序。我在应用程序中有一个连续运行的线程,它下载用户购物篮并将其保存在本地缓存中以提高性能。 到目前为止,我所拥有的是这样的:
while(UserLoggedIn)
{
await Networking.getBasket();
await Task.Delay(5000);
}
该线程在应用程序恢复时启动。我的问题是:控制这样一个线程的最佳方法是什么?在应用程序恢复时它看起来真的很慢,有时它会阻塞 UI 线程,尽管它是完全异步的。我怎样才能提高性能?
编辑
根据Romasz的建议,我使用了这样一个Timer:
声明它:
Timer _getBasketTimer;
在 App 的构造函数中初始化它:
_getBasketTimer = new Timer(getBasketCallback, null, 5000, Timeout.Infinite);
定义回调:
private async void getBasketCallback(Object state)
{
if (JSONCache.getSessionID() != "" && Networking.LoginInProgress == false)
await Networking.getBasket();
//The Change method may be called when the Timer object is already Disposed (I debugged it, and the exception did occur sometimes)
try
{
_getBasketTimer.Change(5000, Timeout.Infinite);
}
catch(ObjectDisposedException)
{
}
}
在App的Suspending事件中配置,当App挂起时Thread不会运行:
_getBasketTimer.Dispose();
并在应用恢复时启动它:
_getBasketTimer = new Timer(getBasketCallback, null, 5000, Timeout.Infinite);
最佳答案
如果不看更多代码就很难判断,但您可能需要将循环包装在一个新线程中以使其脱离 UI 线程:
Task.Run(async () =>
{
while(UserLoggedIn)
{
await Networking.getBasket();
await Task.Delay(5000);
}
});
关于c# - Windows Phone 8.1 应用程序中的后台线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30669384/