Exemple #1
0
        private static void Main(string[] args)
        {
            // create the event loop
            var loop = new EventLoop();

            // start it with a callback
            loop.Start(() => Console.WriteLine("Event loop has started"));

            // start a timer
            loop.SetTimeout(() => Console.WriteLine("Hello, I am a timeout happening after 1 second"), TimeSpan.FromSeconds(1));
            loop.SetInterval(() => Console.WriteLine("Hello, I am an interval happening every 2 seconds"), TimeSpan.FromSeconds(2));

            // read the app.config
            var appConfigFile = new FileInfo("NLoop.TestApp.exe.config");
            var promise = appConfigFile.ReadAllBytes(loop);
            promise.Then(content => Console.WriteLine("Success!! read {0} bytes from app.config", content.Length), reason => Console.WriteLine("Dread!! got an error: {0}", reason));

            // send a web request
            var httpClient = new HttpClient();
            var request = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com/");
            var httpPromise = httpClient.Send(loop, request);
            httpPromise.Then(content => Console.WriteLine("Success!! read {0} from google.com", content.StatusCode), reason => Console.WriteLine("Dread!! got an error: {0}", reason));
            //httpPromise.Cancel();

            // wait
            Console.WriteLine("Event loop is processing, press any key to exit");
            Console.Read();

            // we are done, dispose the loop so resources will be cleaned up
            loop.Dispose();
        }
        public void SetIntervalParameterChecking()
        {
            // arrange
            var loop = new EventLoop();
            loop.Start(() => { });
            var callback = new Action(() => { });
            var timeout = TimeSpan.FromMilliseconds(100);

            // assert
            Assert.That(() => ((EventLoop) null).SetInterval(callback, timeout), Throws.InstanceOf<ArgumentNullException>());
            Assert.That(() => loop.SetInterval(null, timeout), Throws.InstanceOf<ArgumentNullException>());

            // cleanup
            loop.Dispose();
        }
        public void SetInterval()
        {
            // arrange
            var loop = new EventLoop();
            loop.Start(() => { });
            var counter = new CountdownEvent(2);
            var callback = new Action(() => counter.Signal());
            var timeout = TimeSpan.FromMilliseconds(100);

            // act
            loop.SetInterval(callback, timeout);

            // assert
            Assert.That(counter.Wait(timeout + timeout + timeout), Is.True);

            // cleanup
            loop.Dispose();
            counter.Dispose();
        }
        public void SetTimeout()
        {
            // arrange
            var loop = new EventLoop();
            loop.Start(() => { });
            var callbackinvoked = new ManualResetEvent(false);
            var callback = new Action(() => callbackinvoked.Set());
            var timeout = TimeSpan.FromMilliseconds(100);

            // act
            var cancelTimeout = loop.SetTimeout(callback, timeout);

            // assert
            Assert.That(cancelTimeout, Is.Not.Null);
            Assert.That(callbackinvoked.WaitOne(timeout + timeout), Is.True);

            // cleanup
            loop.Dispose();
            callbackinvoked.Dispose();
        }