/// <summary> /// cancellation of the token and stops loading website if currently loading any /// </summary> public void CancelCollectData() { if (StopTS != null && !StopTS.IsCancellationRequested) { if (WebBrowser.IsLoading) { WebBrowser.Stop(); } StopTS.Cancel(); //cancel browsing } }
/// <summary> /// creates new cancellation token, calls LoopCollectingAsync /// finally disposes cancellation token /// </summary> /// <param name="nameList">list of names/phrases</param> /// <param name="webBrowser">cefsharp browser interface</param> /// <param name="searchPhrase">built search phrase</param> public async Task CollectDataAsync(List <string> nameList, IWpfWebBrowser webBrowser, string searchPhrase) { _log.Called(nameList.Count, string.Empty, searchPhrase); if (StopTS == null || StopTS.IsCancellationRequested) //if cancellation token is null or requested, create new one { StopTS = new CancellationTokenSource(); StopCT = StopTS.Token; WebBrowser = webBrowser; SearchPhrase = searchPhrase; NameList = nameList; PhraseNo = GetPhraseNo(); } Task task = Task.Run(async() => { await LoopCollectingAsync(); }, StopCT); try { await task; } catch (OperationCanceledException e) { _log.Info("Cancellation token cancelled: " + e.Message); } catch (Exception e) { _log.Error(e.Message); } finally { StopTS.Dispose(); } StopTS = null; }