예제 #1
0
        private static void ConvertingNonStandardEvents()
        {
            Console.WriteLine();
            Demo.DisplayHeader("Converting Non Standard Events");

            var wifiScanner = new WifiScanner();

            //
            // this code snippet will result in an ArgumentException, since the NetworkFoundEventHandler delegate is not convertible to the standard EventHandler<TEventArgs> type
            //
            //var networks = Observable.FromEventPattern<NetworkFoundEventHandler, string>(
            //      (h) => { wifiScanner.NetworkFound += h; },
            //      (h) => { wifiScanner.NetworkFound -= h; });
            //networks.SubscribeConsole();

            //
            // when the target event has only one parameter, its easy to make it into observable
            //
            //IObservable<string> networks = Observable.FromEvent<NetworkFoundEventHandler, string>(
            //      (h) => { wifiScanner.NetworkFound += h; },
            //      (h) => { wifiScanner.NetworkFound -= h; });

            //
            // When the target event has more than one parameter, a conversion method is needed to turn them into a single object
            //
            IObservable<Tuple<string, int>> networks = Observable
                .FromEvent<ExtendedNetworkFoundEventHandler, Tuple<String, int>>(
                    rxHandler =>
                        (ssid, strength) => rxHandler(Tuple.Create(ssid, strength)),
                    h => wifiScanner.ExtendedNetworkFound += h,
                    h => wifiScanner.ExtendedNetworkFound -= h);


            networks.SubscribeConsole();
            networks.SubscribeConsole();

            while (true)
            {
                Console.WriteLine("Enter the network ssid or X to exit");
                var ssid = Console.ReadLine();
                if (ssid == "X")
                {
                    break;
                }
                Console.WriteLine("Enter the network strength - 1 to 10");
                var strength = int.Parse(Console.ReadLine());

                wifiScanner.RaiseFound(ssid, strength);
            }
        }
예제 #2
0
        private static void ConvertingNonStandardEvents()
        {
            Console.WriteLine();
            Demo.DisplayHeader("Converting Non Standard Events");

            var wifiScanner = new WifiScanner();

            //
            // this code snippet will result in an ArgumentException, since the NetworkFoundEventHandler delegate is not convertible to the standard EventHandler<TEventArgs> type
            //
            //var networks = Observable.FromEventPattern<NetworkFoundEventHandler, string>(
            //      h => wifiScanner.NetworkFound += h,
            //      h => wifiScanner.NetworkFound -= h);
            //networks.SubscribeConsole();

            //
            // when the target event has only one parameter, its easy to make it into observable
            //
            //IObservable<string> networks = Observable.FromEvent<NetworkFoundEventHandler, string>(
            //      h => wifiScanner.NetworkFound += h,
            //      h => wifiScanner.NetworkFound -= h);

            //
            // When the target event has more than one parameter, a conversion method is needed to turn them into a single object
            //
            IObservable <Tuple <string, int> > networks = Observable
                                                          .FromEvent <ExtendedNetworkFoundEventHandler, Tuple <String, int> >(
                rxHandler =>
                (ssid, strength) => rxHandler(Tuple.Create(ssid, strength)),
                h => wifiScanner.ExtendedNetworkFound += h,
                h => wifiScanner.ExtendedNetworkFound -= h);


            networks.SubscribeConsole();
            networks.SubscribeConsole();

            while (true)
            {
                Console.WriteLine("Enter the network ssid or X to exit");
                var ssid = Console.ReadLine();
                if (ssid == "X")
                {
                    break;
                }
                Console.WriteLine("Enter the network strength - 1 to 10");
                var strength = int.Parse(Console.ReadLine());

                wifiScanner.RaiseFound(ssid, strength);
            }
        }
예제 #3
0
        static void ConvertingEventsWithNoArguments()
        {
            Console.WriteLine();
            Demo.DisplayHeader("Converting Events With No Arguments");

            var wifiScanner = new WifiScanner();
            IObservable <Unit> connected = Observable.FromEvent(
                h => wifiScanner.Connected += h,
                h => wifiScanner.Connected -= h);

            connected.SubscribeConsole("connected");

            wifiScanner.RaiseConnected();
            wifiScanner.RaiseConnected();
        }
예제 #4
0
        static void ConvertingEventsWithNoArguments()
        {
            Console.WriteLine();
            Demo.DisplayHeader("Converting Events With No Arguments");

            var wifiScanner = new WifiScanner();
            IObservable<Unit> connected = Observable.FromEvent(
                h => wifiScanner.Connected += h,
                h => wifiScanner.Connected -= h);

            connected.SubscribeConsole("connected");

            wifiScanner.RaiseConnected();
            wifiScanner.RaiseConnected();


        }