/// <summary> /// 开始更新。 /// </summary> public static async void Update() { if (Status != UpdateStatus.ReadyToUpdate) { return; } try { foreach (var item in _updateItems) { item.FilePath = Path.GetTempFileName(); _webClient.DownloadFileAsync(item.DownloadUri, item.FilePath); while (_webClient.IsBusy) { await Task.Delay(500); } } UpdatesAllDownloaded(null, null); File.WriteAllBytes(_installerPath, Properties.Resources.ZipExtractor); while (_updateItems.Count > 0) { UpdateItem item = _updateItems.Dequeue(); Install(item); } Status = _needManualUpdate ? UpdateStatus.NeedManualUpdate : UpdateStatus.UpToDate; var args = new UpdateStatusChangedEventArgs(Status); StatusChanged(null, args); } catch (Exception e) { Status = UpdateStatus.Error; var args = new UpdateStatusChangedEventArgs(Status, e); StatusChanged(null, args); } }
/// <summary> /// 开始检查更新。 /// </summary> public static async void CheckAsync() { if (Status != UpdateStatus.NotStarted) { return; } try { await CheckUpdateAsync(); if (_updateItems.Count > 0) { Status = UpdateStatus.ReadyToUpdate; } else if (_needManualUpdate) { Status = UpdateStatus.NeedManualUpdate; } else { Status = UpdateStatus.UpToDate; } var args = new UpdateStatusChangedEventArgs(Status); StatusChanged(null, args); } catch (Exception e) { if (e is WebException) { _changelog = "与服务器通信时出错。"; } else { _changelog = $"异常:{e}\n{e.Message}"; } Status = UpdateStatus.Error; var args = new UpdateStatusChangedEventArgs(Status, e); StatusChanged(null, args); } }