Пример #1
0
        /// <summary>
        /// Subscribes to this event such that the given handler will be called when the event fires
        /// </summary>
        /// <param name="handler">the action to run when the event fires</param>
        /// <returns>A subscription that can be disposed when you no loner want to be notified from this event</returns>
        public ILifetime SubscribeUnmanaged(Action handler)
        {
            var sub = new Lifetime();

            sub.OnDisposed(() => subscribers.Remove(handler));
            subscribers.Add(handler, sub);
            return(sub);
        }
Пример #2
0
        private static Lifetime CreateForeverLifetime()
        {
            var ret = new Lifetime();

            ret.OnDisposed(() =>
            {
                throw new Exception("Forever lifetime expired");
            });

            return(ret);
        }
Пример #3
0
        /// <summary>
        /// Starts simulated human output mode.
        /// </summary>
        /// <returns>A lifetime that you can dispose when you want to stop the effect.</returns>
        public static Lifetime Start()
        {
            var  writer = new SimulatedHumanOutputWriter();
            var  lt     = new Lifetime();
            Task t      = new Task(() =>
            {
                Thread.CurrentThread.IsBackground = false;
                writer.Loop();
            });

            lt.OnDisposed(writer.Cancel);
            t.Start();
            Thread.Sleep(100);
            return(lt);
        }