AsyncAwesome.AsyncShowcase.addValue C# (CSharp) Method

addValue() public method

public addValue ( int addVal ) : Task
addVal int
return Task
        public async Task addValue(int addVal) 
        {
            int newVal; // newVal can be assigned using either an awaited task or a synchronous operation
            if (addVal % 2 == 0)
            {
                newVal = await Task.Run(() => Value + addVal);
            }
            else
            {
                newVal = Value + addVal;
            }

            Value = newVal; // running this via a continuation or in line both work
        }

Usage Example

コード例 #1
0
        private async static Task AsyncMain()
        {
            var asyncShow = new AsyncShowcase();

            /*
             * A task can be started and not immediatley awaited.
             */
            var t = Task.Run(() =>
            {
                Thread.Sleep(1000);
                asyncShow.addValuesInRange(0, 10).Wait();
            });

            await asyncShow.addValue(10);                       // add 10 and wait

            Console.WriteLine(await asyncShow.getValueAsync()); // can wait within function call and function will receive argument

            await t;

            if (await asyncShow.getValueAsync() == 55) // I can use await within a conditional
            {
                Console.WriteLine("Success!");
            }
        }
All Usage Examples Of AsyncAwesome.AsyncShowcase::addValue