-2

i want to return a Task <List> . via c# and .net 4.0 vs2019. don't useing asyns.

        CancellationTokenSource source = new CancellationTokenSource();
        Task  List<AccountList> task1 = new Task List<AccountList>(() => {
           using (var db1 = new SqlConnection(DbHelper.AccountConStr))        

            {
                string sql = "select * from  AccountList where UseFlag='1'";
                try
                {
                    IEnumerable<AccountList> lst = db1.Query<AccountList>(sql);
                    return lst.ToList();
                }
                catch
                {
                    return new List<AccountList>();
                }
            }
        });
        task1.Start();
        while (timeout > sw.ElapsedMilliseconds)
        source.Cancel();
       

how to return a Task <List>

2
  • Your question is not that clear but you can return task based result using async await... please check the official doc for this via this link c-sharpcorner.com/article/async-and-await-in-c-sharp
    – vijay sahu
    Jul 11, 2020 at 16:35
  • The example uses async My environment is. Net 4.0. Async cannot be used
    – kkaa
    Jul 11, 2020 at 16:42

1 Answer 1

0

Why can't you return a

Task<List<AccountList>>

i.e just return task1.

Then in the calling method as you do not have async/await, you can achieve the same thing by using Task.ContinueWith(...). The stuff you want to do when the task resolves goes inside the ContinueWith. Async / Await is just synatx sugar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.