public async void StartUpdate() { try { var taskCount = 3; var groups = NeedUpdateCatalogs.Split <BookCatalog>(taskCount); var enumerable = groups as IEnumerable <BookCatalog>[] ?? groups.ToArray(); if (groups == null || !enumerable.Any()) { return; } IsUpdating = true; var tasks = new Task[enumerable.Length]; for (var i = 0; i < enumerable.Length; i++) { if (IsDeleted) { break; } tasks[i] = await Task.Factory.StartNew(async() => { var catalogs = enumerable[i]; foreach (var bookCatalog in catalogs) { try { if (IsDeleted) { break; } bookCatalog.CatalogContent = await AnalisysSourceHelper.GetCatalogContent(bookCatalog.CatalogUrl); } catch (Exception e) { Console.WriteLine(e); } finally { lock (_obj) { DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = (int.Parse(NeedUpdateCount) - 1).ToString(); }); } } } }); } await Task.Factory.ContinueWhenAll(tasks, (obj) => { DbHelper.AddDbOperator(new Action(() => { var reslut = DbLocalBook.InsertOrUpdateBookCatalogs(AppDataPath.GetLocalBookDbPath(), NeedUpdateCatalogs); })); DispatcherHelper.CheckBeginInvokeOnUI(() => { NeedUpdateCount = ""; }); }); } catch (Exception e) { Console.WriteLine(e); } finally { IsUpdating = false; } }