我在C#Xamarin代码中得到了这个神秘异常:
// startTime contains DateTime.Now set earlier in code
//_progressNotification and _..Manager were set once earlier and are never null
//The various Duration variables are doubles with values >=0 (totalDuration is never zero)
// The following code is inside var progressHandler = new Progress<double>(p =>
var progress = (int)Math.Round((p*video.Duration.TotalSeconds + downloadedDuration) * (100d/totalDuration));
progress = Math.Min(progress, 100);
progress = Math.Max(progress, 0);
var secondsPasssed = (DateTime.Now - startTime).TotalSeconds; // For how many seconds are we downloading
var remainingTine = (downloadedDuration + p * video.Duration.TotalSeconds) <= 0 ? "??:??:??" : TimeSpan.FromSeconds((secondsPasssed * totalDuration / (p * video.Duration.TotalSeconds + downloadedDuration)) - secondsPasssed).ToString(@"hh\:mm\:ss");
_progressNotification.SetContentText($"{i}/{totalItemsInQueue} {remainingTine}");
_progressNotification.SetProgress(100, progress, false);
_notificationManager.Notify(ProgressNotificationId, _progressNotification.Build()); // This line throws the exception
基本上,进度方法在0到1(p)之间执行,然后进行一些简单的数学运算来计算进度值,然后发布(更新)显示此进度的通知
它在大多数情况下都能很好地工作,但有时会抛出上面提到的神秘异常。
我无法弄清楚导致异常发生的原因。