ユーザ用ツール

サイト用ツール


programming:async・await・taskによる非同期処理・同期処理

実行結果

$ dotnet run
This is MethodSample - a.
This is Main method - a.
This is MethodSample - b.
This is Main method - b.

ソース

Program.cs
using System;
using System.Threading.Tasks;
using System.Threading;
 
namespace console
{
    class Program
    {
        static void Main(string[] args)
        {
            AsyncAwaitSample Sample = new AsyncAwaitSample();
            Sample.MethodSample();
            Console.WriteLine("This is Main method - a.");
            Thread.Sleep( 3000 );
            Console.WriteLine("This is Main method - b.");
        }
    }
 
    public class AsyncAwaitSample
    {
        // This method is asyncronized method.
        // so, after "await" return to Main method immediately.
        public async void MethodSample()
        {
            Console.WriteLine("This is MethodSample - a.");
            await Task.Run( () => {
                Thread.Sleep( 2000 );
            } );
            Console.WriteLine("This is MethodSample - b.");
        }
    }
}
programming/async・await・taskによる非同期処理・同期処理.txt · 最終更新: 2017/08/09 05:24 by dot