Ejemplo n.º 1
0
        public void MoveNext()
        {
            try
            {
                if (this.State == State.BeforeProcessing)
                {
                    this.Laundry      = new AsyncLaundry();
                    this.DirtyClothes = new Clothes();

                    Console.WriteLine("Received a pile of dirty clothes.");

                    this.WashingAwaiter = this.Laundry.Wash(this.DirtyClothes).GetAwaiter();

                    this.State = State.AfterWashing;

                    if (this.WashingAwaiter.IsCompleted == false)
                    {
                        this.WashingAwaiter.OnCompleted(this.MoveNext);
                        return;
                    }
                }

                if (this.State == State.AfterWashing)
                {
                    this.WetCleanClothes = this.WashingAwaiter.GetResult();

                    Console.WriteLine("Clothes washed!");

                    this.DryingAwaiter = this.Laundry.Dry(this.WetCleanClothes).GetAwaiter();

                    this.State = State.AfterDrying;

                    if (this.DryingAwaiter.IsCompleted == false)
                    {
                        this.DryingAwaiter.OnCompleted(this.MoveNext);
                        return;
                    }
                }

                if (this.State == State.AfterDrying)
                {
                    this.DryCleanClothes = this.DryingAwaiter.GetResult();

                    Console.WriteLine("Clothes dried!");
                    Console.WriteLine("Clothes ready to collect!");
                }
            }
            catch (Exception ex)
            {
                this.State = State.Finished;
                this.TaskCompletionSource.SetException(ex);
                return;
            }

            this.State = State.Finished;
            TaskCompletionSource.SetResult(this.DryCleanClothes);
        }
Ejemplo n.º 2
0
        public async Task Run()
        {
            var laundry      = new AsyncLaundry();
            var dirtyClothes = new Clothes();

            Console.WriteLine("Entering the laundry.");

            var wetCleanClothes = await laundry.Wash(dirtyClothes);

            Console.WriteLine("Got clean but wet clothes.");

            var dryCleanClothes = await laundry.Dry(wetCleanClothes);

            Console.WriteLine("Got clean and dry clothes.");
            Console.WriteLine("Leaving the laundry.");
        }