Exemple #1
0
        public async Task TwoObjects_OneCancelled()
        {
            var obj1 = new ObjectWithEvent();
            var t1   = _eas.Create(obj1);

            var obj2 = new ObjectWithEvent();
            var cts  = new CancellationTokenSource();
            var t2   = _eas.Create(obj2, cts.Token);

            t1.IsCompleted.Should().BeFalse();
            t2.IsCompleted.Should().BeFalse();

            Task.Run(() => obj1.Raise(5)).DoNotWait();

            await Task.WhenAny(t1, t2);

            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeFalse();
            t1.Result.Should().BeOfType <IntEventArgs>()
            .Which.Value.Should().Be(5);

            Task.Run(() => cts.Cancel()).DoNotWait();
            Func <Task> f = async() => await t2;

            f.ShouldThrow <OperationCanceledException>();

            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeTrue();
        }
        public async Task TwoObjects_OneCancelled() {
            var obj1 = new ObjectWithEvent();
            var t1 = _eas.Create(obj1);

            var obj2 = new ObjectWithEvent();
            var cts = new CancellationTokenSource();
            var t2 = _eas.Create(obj2, cts.Token);

            t1.IsCompleted.Should().BeFalse();
            t2.IsCompleted.Should().BeFalse();

            Task.Run(() => obj1.Raise(5)).DoNotWait();

            await Task.WhenAny(t1, t2);
            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeFalse();
            t1.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(5);

            Task.Run(() => cts.Cancel()).DoNotWait();
            Func<Task> f = async () => await t2;
            f.ShouldThrow<OperationCanceledException>();

            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeTrue();
        }
        public async Task TwoObjects() {
            var obj1 = new ObjectWithEvent();
            var obj2 = new ObjectWithEvent();
            var t1 = _eas.Create(obj1);
            var t2 = _eas.Create(obj2);

            t1.IsCompleted.Should().BeFalse();
            t2.IsCompleted.Should().BeFalse();

            Task.Run(() => obj1.Raise(5)).DoNotWait();

            await Task.WhenAny(t1, t2);
            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeFalse();
            t1.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(5);
            
            Task.Run(() => obj2.Raise(10)).DoNotWait();

            await t2;
            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeTrue();

            t2.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(10);
        }
Exemple #4
0
        private static void Example1()
        {
            // first we allocate some objects. the second object we create 
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent = new ObjectWithEvent();
            var objectThatHooksEvent = new ObjectThatHooksEvent(objectWithEvent);

            // we'll set the instance to null and call the GC, but since we 
            // have an event still hooked up to the first object, no objects 
            // will get finalized yet.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();
            Console.WriteLine("Nothing magical?");

            // let's try unhooking the event now and calling the gc. we should
            // finally be able to get rid of our second object
            Console.WriteLine();
            Console.WriteLine("Press enter and I'll unhook the events for you and call the garbage collector again.");
            Console.ReadLine();
            objectWithEvent.UnhookAll();
            GC.Collect();

            // now that we've ditched the second object, we can safely get rid
            // of the first one!
            Console.WriteLine("Neat-o, eh? Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }
Exemple #5
0
        private static void Example1()
        {
            // first we allocate some objects. the second object we create
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent      = new ObjectWithEvent();
            var objectThatHooksEvent = new ObjectThatHooksEvent(objectWithEvent);

            // we'll set the instance to null and call the GC, but since we
            // have an event still hooked up to the first object, no objects
            // will get finalized yet.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();
            Console.WriteLine("Nothing magical?");

            // let's try unhooking the event now and calling the gc. we should
            // finally be able to get rid of our second object
            Console.WriteLine();
            Console.WriteLine("Press enter and I'll unhook the events for you and call the garbage collector again.");
            Console.ReadLine();
            objectWithEvent.UnhookAll();
            GC.Collect();

            // now that we've ditched the second object, we can safely get rid
            // of the first one!
            Console.WriteLine("Neat-o, eh? Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }
Exemple #6
0
        public async Task TwoObjects()
        {
            var obj1 = new ObjectWithEvent();
            var obj2 = new ObjectWithEvent();
            var t1   = _eas.Create(obj1);
            var t2   = _eas.Create(obj2);

            t1.IsCompleted.Should().BeFalse();
            t2.IsCompleted.Should().BeFalse();

            Task.Run(() => obj1.Raise(5)).DoNotWait();

            await Task.WhenAny(t1, t2);

            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeFalse();
            t1.Result.Should().BeOfType <IntEventArgs>()
            .Which.Value.Should().Be(5);

            Task.Run(() => obj2.Raise(10)).DoNotWait();

            await t2;

            t1.IsCompleted.Should().BeTrue();
            t2.IsCompleted.Should().BeTrue();

            t2.Result.Should().BeOfType <IntEventArgs>()
            .Which.Value.Should().Be(10);
        }
Exemple #7
0
        private static void Example2()
        {
            // first we allocate some objects. the second object we create
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent      = new ObjectWithEvent();
            var objectThatHooksEvent = new HookWithAnonymousDelegate(objectWithEvent);

            // we'll set the instance to null and call the GC, and since we
            // our event handler we've set up doesn't have any dependencies on
            // the object we've hooked with, it will get cleaned up.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();

            // the first object should be just as easy to get rid of!
            Console.WriteLine("Look at that! Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }
Exemple #8
0
 public HookWithAnonymousDelegate2(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += (sender, args) =>
     {
         // handle your event and use something that's part of this instance
         SomeInnocentLittleMethod();
     };
 }
Exemple #9
0
 public HookWithAnonymousDelegate(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += (sender, args) =>
     {
         // handle your event
         // (this one is special because it doesn't use anything related to the instance)
         Console.WriteLine("Event being called!");
     };
 }
        public async Task TwoTasks() {
            var obj = new ObjectWithEvent();
            var t1 = _eas.Create(obj);
            var t2 = _eas.Create(obj);

            t1.IsCompleted.Should().BeFalse();
            t2.IsCompleted.Should().BeFalse();
            Task.Run(() => obj.Raise(5)).DoNotWait();

            await Task.WhenAll(t1, t2);
            t1.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(5);
            t2.Result.Should().Be(t1.Result);
        }
Exemple #11
0
            public async Task TwoTasks()
            {
                var obj = new ObjectWithEvent();
                var t1  = _eas.Create(obj);
                var t2  = _eas.Create(obj);

                t1.IsCompleted.Should().BeFalse();
                t2.IsCompleted.Should().BeFalse();
                Task.Run(() => obj.RaiseInt(5)).DoNotWait();

                await Task.WhenAll(t1, t2);

                t1.Result.Value.Should().Be(5);
                t2.Result.Should().Be(t1.Result);
            }
Exemple #12
0
            public async Task TwoEvents_NoUnsubscribe()
            {
                var obj = new ObjectWithEvent();
                var t   = _eas.Create(obj);

                t.IsCompleted.Should().BeFalse();
                Task.Run(() => obj.RaiseInt(5)).DoNotWait();

                await t;

                t.Result.Value.Should().Be(5);

                Task.Run(() => obj.RaiseInt(10)).DoNotWait();
                t.Result.Value.Should().Be(5);
            }
Exemple #13
0
        public async Task TwoEvents()
        {
            var obj = new ObjectWithEvent();
            var t   = _eas.Create(obj);

            t.IsCompleted.Should().BeFalse();
            Task.Run(() => obj.Raise(5)).DoNotWait();

            await t;

            t.Result.Should().BeOfType <IntEventArgs>()
            .Which.Value.Should().Be(5);

            Task.Run(() => obj.Raise(10)).DoNotWait();
            t.Result.Should().BeOfType <IntEventArgs>()
            .Which.Value.Should().Be(5);
        }
Exemple #14
0
            public async Task TwoEventsSequentalTasks()
            {
                var obj = new ObjectWithEvent();
                var t1  = _eas.Create(obj);

                t1.IsCompleted.Should().BeFalse();
                Task.Run(() => obj.RaiseInt(5)).DoNotWait();

                await t1;

                t1.Result.Value.Should().Be(5);

                var t2 = _eas.Create(obj);

                Task.Run(() => obj.RaiseInt(10)).DoNotWait();
                t1.Result.Value.Should().Be(5);
                t2.Result.Value.Should().Be(10);
            }
Exemple #15
0
 public ObjectThatHooksEvent(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += ObjectWithEvent_Event;
 }
Exemple #16
0
 public HookWithAnonymousDelegate2(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += (sender, args) =>
     {
         // handle your event and use something that's part of this instance
         SomeInnocentLittleMethod();
     };
 }
Exemple #17
0
        private static void Example2()
        {
            // first we allocate some objects. the second object we create 
            // depends on the first. the second object is going to hook onto
            // the event exposed by the first object.
            Console.WriteLine("Creating instances...");
            var objectWithEvent = new ObjectWithEvent();
            var objectThatHooksEvent = new HookWithAnonymousDelegate(objectWithEvent);

            // we'll set the instance to null and call the GC, and since we 
            // our event handler we've set up doesn't have any dependencies on
            // the object we've hooked with, it will get cleaned up.
            Console.WriteLine();
            Console.WriteLine("Setting the object that hooks the event to null and calling garbage collector...");
            objectThatHooksEvent = null;
            GC.Collect();

            // the first object should be just as easy to get rid of!
            Console.WriteLine("Look at that! Press enter and I'll set the object with the event to null and call the garbage collector one last time.");
            Console.ReadLine();
            objectWithEvent = null;
            GC.Collect();

            Console.WriteLine("And presto! Both are gone.");
        }
            public async Task TwoEventsSequentalTasks() {
                var obj = new ObjectWithEvent();
                var t1 = _eas.Create(obj);

                t1.IsCompleted.Should().BeFalse();
                Task.Run(() => obj.RaiseInt(5)).DoNotWait();

                await t1;
                t1.Result.Value.Should().Be(5);

                var t2 = _eas.Create(obj);
                Task.Run(() => obj.RaiseInt(10)).DoNotWait();
                t1.Result.Value.Should().Be(5);
                t2.Result.Value.Should().Be(10);
            }
Exemple #19
0
 public ObjectThatHooksEvent(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += ObjectWithEvent_Event;
 }
            public async Task TwoEvents_NoUnsubscribe() {
                var obj = new ObjectWithEvent();
                var t = _eas.Create(obj);

                t.IsCompleted.Should().BeFalse();
                Task.Run(() => obj.RaiseInt(5)).DoNotWait();

                await t;
                t.Result.Value.Should().Be(5);

                Task.Run(() => obj.RaiseInt(10)).DoNotWait();
                t.Result.Value.Should().Be(5);
            }
        public async Task TwoEvents() {
            var obj = new ObjectWithEvent();
            var t = _eas.Create(obj);

            t.IsCompleted.Should().BeFalse();
            Task.Run(() => obj.Raise(5)).DoNotWait();

            await t;
            t.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(5);

            Task.Run(() => obj.Raise(10)).DoNotWait();
            t.Result.Should().BeOfType<IntEventArgs>()
                .Which.Value.Should().Be(5);
        }
Exemple #22
0
 public HookWithAnonymousDelegate(ObjectWithEvent objectWithEvent)
 {
     objectWithEvent.Event += (sender, args) =>
     {
         // handle your event
         // (this one is special because it doesn't use anything related to the instance)
         Console.WriteLine("Event being called!");
     };
 }