【发布时间】:2022-11-21 11:39:49
【问题描述】:
我有一个前台服务,每 5 秒获取一次设备的位置并将其发送到服务器,但一段时间后,前台服务停止获取地理定位数据。
public async Task RunLocation(CancellationToken token)
{
await Task.Run(async () =>
{
if (token.IsCancellationRequested)
_stopping = true;
while (!_stopping)
{
token.ThrowIfCancellationRequested();
try
{
await Task.Delay(5000, token);
var request = new GeolocationRequest(GeolocationAccuracy.Best, TimeSpan.FromSeconds(5));
var location = await Geolocation.Default.GetLocationAsync(request, token);
Console.WriteLine("Got location: " + location);
}}}}
【问题讨论】:
-
为什么你有一个没有
catch的try块?这样做的目的是什么?你怎么知道是否有例外? -
也许未处理的异常?你怎么知道这个循环还在运行?
标签: maui