private void btnParseAmazonCode_Click(object sender, RoutedEventArgs e) { //string regex = @"USD ([$\d\.]*)[\s]*Amazon.com Gift Card claim code: ([\d\w\-]*)"; string regex = @"\$([\d\.]*).([\w\d]{4}-[\w\d]{6}-[\w\d]{4})"; MatchCollection matches = Regex.Matches(txtUnparsedAmazonCodes.Text, regex); bool noValue = false; if (matches.Count < 1) { regex = @"([\w\d]{4}-[\w\d]{6}-[\w\d]{4})"; matches = Regex.Matches(txtUnparsedAmazonCodes.Text, regex); noValue = true; } StringBuilder sb = new StringBuilder(); colParsedAmazonGiftCodes.Clear(); int codeCount = 0; foreach (Match m in matches) { codeCount++; if (noValue) { colParsedAmazonGiftCodes.Add(new AmazonGiftCode(codeCount, m.Groups[1].Value, null)); } else { colParsedAmazonGiftCodes.Add(new AmazonGiftCode(codeCount, m.Groups[2].Value, decimal.Parse(m.Groups[1].Value))); } } if (colParsedAmazonGiftCodes.Count < 1) { txtUnparsedAmazonCodes.Background = System.Windows.Media.Brushes.Pink; } else { txtExpectedValue.Text = colParsedAmazonGiftCodes.Sum(p => p.Value).ToString(); if (txtExpectedValue.Text == String.Empty) { txtExpectedValue.Background = System.Windows.Media.Brushes.Pink; } else { var listDuplicateGiftCodes = colParsedAmazonGiftCodes.GroupBy(p => p.Code).Where(g => g.Count() > 1).Select(p => p.Key).ToList(); if (listDuplicateGiftCodes.Count > 0) { sb = new StringBuilder(); sb.AppendLine("Duplicate gift codes detected!"); foreach (var gc in listDuplicateGiftCodes) { sb.AppendLine(gc); } MessageBox.Show(sb.ToString()); } decimal expectedValue = 0; decimal.TryParse(txtExpectedValue.Text, out expectedValue); if (expectedValue > 0) { txtExpectedValue.Background = System.Windows.Media.Brushes.White; } else { txtExpectedValue.Background = System.Windows.Media.Brushes.Pink; } } txtUnparsedAmazonCodes.Background = System.Windows.Media.Brushes.White; } //txtExpectedValue.Focus(); //txtExpectedValue.SelectAll(); }
private static void Main(string[] args) { var autoReset = new AutoResetEvent(false); var r = new Random(); var o = new TrulyObservableCollection <DataPoint>(); var subscription1 = Observable.Interval(TimeSpan.FromSeconds(1)).Take(3).Subscribe( i => { o.Add( new DataPoint { ItemCount = r.Next(100) }); Console.WriteLine("Fire1 {0}", i); }); var subscription2 = Observable.FromEventPattern <NotifyCollectionChangedEventArgs>(o, "CollectionChanged") .Subscribe(s => { Console.WriteLine("List changed. Current total {0}", o.Sum(s1 => s1.ItemCount)); }); var subscription3 = Observable.Interval(TimeSpan.FromSeconds(1)).Delay(TimeSpan.FromSeconds(3)).Take(3).Finally( () => { o.Clear(); autoReset.Set(); }).Subscribe( i => { if (o.Any()) { o[r.Next(o.Count)].ItemCount = r.Next(100); Console.WriteLine("Fire3 {0}", i); } }); autoReset.WaitOne(); }