private void buCheck_Click(object sender, RoutedEventArgs e) { if (!_isChecking) { SetWorkState(WorkState.Checking); _cancel = new CancellationTokenSource(); Task <List <Tuple <string, string> > > .Factory.StartNew(PrepareDomains, new Tuple <object, string>(tbDomains.Tag, tbDomains.Text)) .ContinueWith((x) => { _isChecking = true; var checkApi = new CheckApi_RWEN(); Checking(checkApi, x.Result); _isChecking = false; }, _cancel.Token) .ContinueWith((x) => { // IsFaulted must before IsCompleted if (x.IsFaulted) { if (x.Exception != null) { var err = string.Empty; var tmp = x.Exception.InnerExceptions.ToList(); tmp.ForEach((ex) => { err += ex.Message + Environment.NewLine; }); _errorStream.WriteLine("Got error: {0}", err); MessageBox.Show(err); } // should not occor. else { _errorStream.WriteLine("Got unknown error."); MessageBox.Show("Got unknown error."); } } else if (x.IsCanceled) { SetWorkState(WorkState.CheckCanceled); } else if (x.IsCompleted) { SetWorkState(WorkState.CheckCompleted); } }); } else { _isChecking = false; _cancel.Cancel(); SetWorkState(WorkState.CheckCanceling); } }
private void buCheck_Click(object sender, RoutedEventArgs e) { if (!_isChecking) { SetWorkState(WorkState.Checking); _cancel = new CancellationTokenSource(); Task<List<Tuple<string, string>>>.Factory.StartNew(PrepareDomains, new Tuple<object, string>(tbDomains.Tag, tbDomains.Text)) .ContinueWith((x) => { _isChecking = true; var checkApi = new CheckApi_RWEN(); Checking(checkApi, x.Result); _isChecking = false; }, _cancel.Token) .ContinueWith((x) => { // IsFaulted must before IsCompleted if (x.IsFaulted) { if (x.Exception != null) { var err = string.Empty; var tmp = x.Exception.InnerExceptions.ToList(); tmp.ForEach((ex) => { err += ex.Message + Environment.NewLine; }); _errorStream.WriteLine("Got error: {0}", err); MessageBox.Show(err); } // should not occor. else { _errorStream.WriteLine("Got unknown error."); MessageBox.Show("Got unknown error."); } } else if (x.IsCanceled) { SetWorkState(WorkState.CheckCanceled); } else if (x.IsCompleted) { SetWorkState(WorkState.CheckCompleted); } }); } else { _isChecking = false; _cancel.Cancel(); SetWorkState(WorkState.CheckCanceling); } }