async/await keywords not available in .net 4.0

前端 未结 4 575
清歌不尽
清歌不尽 2020-12-03 04:39

I would like to use the async/await in C# 4.0 and I have installed the following package:

http://www.nuget.org/packages/Microsoft.Bcl.Async/

The problem is t

4条回答
  •  一整个雨季
    2020-12-03 05:20

    using System.Threading.Tasks;
    
    private void simpleMethod()
    {
        var tsk = Task.Factory.StartNew(() => DoSomeWorkAsync());
        Task.WaitAll(tsk);
        DataTable table = tsk.Result;
    }
    

    It is important that the asynchronous method should not contain any method that affects form controls

        private DataTable DoSomeWorkAsync()
        {           
            System.Data.DataTable table = new System.Data.DataTable();
            Thread.Sleep(4000); // Any long time process
            return table;
        }
    

    More information: https://www.simplethread.com/net-40-and-systemthreadingtasks/

提交回复
热议问题