Last active
August 29, 2015 13:56
-
-
Save JohanLarsson/9236077 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NreDemo : INotifyPropertyChanged | |
| { | |
| private int _value; | |
| private int _count; | |
| public event PropertyChangedEventHandler PropertyChanged; | |
| public int Value | |
| { | |
| get { return _value; } | |
| set | |
| { | |
| if (value == _value) | |
| return; | |
| _value = value; | |
| OnPropertyChanged(); | |
| } | |
| } | |
| [Test] | |
| public async Task NreTest() | |
| { | |
| PropertyChanged += OnPropertyChanged; | |
| string s = null; | |
| var mre = new ManualResetEvent(false); | |
| Task.Run(() => | |
| { | |
| try | |
| { | |
| Value = 2; | |
| } | |
| catch (Exception e) | |
| { | |
| s = e.Message; | |
| } | |
| finally | |
| { | |
| mre.Set(); | |
| } | |
| }); | |
| await Task.Delay(200); | |
| PropertyChanged -= OnPropertyChanged; | |
| mre.WaitOne(); | |
| Assert.AreEqual("Object reference not set to an instance of an object.", s); | |
| } | |
| private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) | |
| { | |
| _count++; | |
| } | |
| [NotifyPropertyChangedInvocator] | |
| protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
| { | |
| if (PropertyChanged != null) | |
| { | |
| Thread.Sleep(500); | |
| PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NreDemoloop : INotifyPropertyChanged | |
| { | |
| private int _value; | |
| private int _count1; | |
| private int _count2; | |
| public event PropertyChangedEventHandler PropertyChanged; | |
| public int Value | |
| { | |
| get { return _value; } | |
| set | |
| { | |
| if (value == _value) | |
| return; | |
| _value = value; | |
| OnPropertyChanged(); | |
| } | |
| } | |
| [Test] | |
| public void NreTest() | |
| { | |
| var ints = new List<Tuple<int,int>>(); | |
| for (int i = 0; i < 1000; i++) | |
| { | |
| _count1 = 0; | |
| _count2 = 0; | |
| bool noException = true; | |
| string s = null; | |
| var mre = new ManualResetEvent(false); | |
| Task.Run(() => | |
| { | |
| while (noException) | |
| { | |
| try | |
| { | |
| Value = _count1; | |
| _count1++; | |
| Thread.Sleep(1); // Adding this sleep makes the nre happen more often, why? | |
| } | |
| catch (Exception e) | |
| { | |
| ints.Add(Tuple.Create( _count1,_count2)); | |
| //ints[i] = _count1; | |
| noException = false; | |
| mre.Set(); | |
| } | |
| } | |
| }); | |
| Task.Run(() => | |
| { | |
| while (noException) | |
| { | |
| PropertyChanged += OnPropertyChanged; | |
| PropertyChanged -= OnPropertyChanged; | |
| _count2++; | |
| } | |
| }); | |
| mre.WaitOne(); | |
| } | |
| //Console.WriteLine(string.Join(", ", ints)); | |
| var ones = ints.Select(x => x.Item1).Average(); | |
| var twos = ints.Select(x => x.Item2).Average(); | |
| Console.WriteLine(string.Format("n: {0} average: {1} count1: {2} count2: {3}", ints.Count, ones,ones,twos)); // n: 1000 average:214,071 | |
| } | |
| private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) | |
| { | |
| } | |
| [NotifyPropertyChangedInvocator] | |
| protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
| { | |
| if (PropertyChanged != null) | |
| { | |
| PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment