简体   繁体   中英

How to return a list to caller method using async method in C#

Can anyone help, how can I return the list to caller method in asynchronous function.

Here is my code, I want to return list named as ResultList

public class ResultData
{
    public int Status { get; set; }
    public string Description { get; set; }
    public int BatchId { get; set; }
}

private static List<Data> ResultList = new List<Data>();

public void TSReCalculation(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
{
     Common.callSpForTS(piGroupId, piIssuerId, pdFromDate, ldToDate, piFormulaId, piIsProcessedType, piIsSeriesReStart, piIsPLCalculate, piIsIncludeEditedScoreint);
}

public static async void callSpForTS(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
{
            await Task.Run(() =>
            {
                    try
                    {
                        sqlCommand.ExecuteNonQuery();
                        dbModel.ResultData item = new dbModel.ResultData();
                        item.Description= "succesfulll"
                        item.Status = 1;
                        item.BatchId = 100;
                        ResultList.Add(item);
                    }
                    catch (Exception ex)
                    {
                        Common.showMessageBox(ex.Message + "InnerException:" + ex.InnerException, MSG_TYP.Error, "Error Occured");
                    }
            });
        }
}

instead of public static async void callSpForTS ie instead of void return type you should keep return type public static async List<type> callSpForTS , you dont need common static list in your code.


Async All the Way

Asynchronous code reminds me of the story of a fellow who mentioned that the world was suspended in space and was immediately challenged by an elderly lady claiming that the world rested on the back of a giant turtle. When the man enquired what the turtle was standing on, the lady replied, “You're very clever, young man, but it's turtles all the way down!” As you convert synchronous code to asynchronous code, you'll find that it works best if asynchronous code calls and is called by other asynchronous code—all the way down (or “up,” if you prefer). Others have also noticed the spreading behavior of asynchronous programming and have called it “contagious” or compared it to a zombie virus. Whether turtles or zombies, it's definitely true that asynchronous code tends to drive surrounding code to also be asynchronous. This behavior is inherent in all types of asynchronous programming, not just the new async/await keywords.

“Async all the way” means that you shouldn't mix synchronous and asynchronous code without carefully considering the consequences. In particular, it's usually a bad idea to block on async code by calling Task.Wait or Task.Result. This is an especially common problem for programmers who are “dipping their toes” into asynchronous programming, converting just a small part of their application and wrapping it in a synchronous API so the rest of the application is isolated from the changes. Unfortunately, they run into problems with deadlocks. After answering many async-related questions on the MSDN forums, Stack Overflow and e-mail, I can say this is by far the most-asked question by async newcomers once they learn the basics: “Why does my partially async code deadlock?”

Refer this : Async/Await - Best Practices in Asynchronous Programming

apart from that you need to async/await in calling function also

public async void TSReCalculation(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
{
    var returnvalue =await Common.callSpForTS(piGroupId, piIssuerId, pdFromDate, ldToDate, piFormulaId, piIsProcessedType, piIsSeriesReStart, piIsPLCalculate, piIsIncludeEditedScoreint);
}

public static async Task<List<ResultData>> callSpForTS(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
{
         return await Task.Run(() =>
            {
                List<ResultData> ResultList = new List<ResultData>();
                    try
                    {
                        sqlCommand.ExecuteNonQuery();
                        dbModel.ResultData item = new dbModel.ResultData();
                        item.Description= "succesfulll"
                        item.Status = 1;
                        item.BatchId = 100;
                        ResultList.Add(item);
                    }
                    catch (Exception ex)
                    {
                        Common.showMessageBox(ex.Message + "InnerException:" + ex.InnerException, MSG_TYP.Error, "Error Occured");
                    }
                 return ResultList;
            });
        }
}
        public async void TSReCalculation(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
        {
            var result = await Common.callSpForTS(piGroupId, piIssuerId, pdFromDate, ldToDate, piFormulaId, piIsProcessedType, piIsSeriesReStart, piIsPLCalculate, piIsIncludeEditedScoreint);
        }


        public static async Task<List<ResultData>> callSpForTS(int piGroupId, int piIssuerId, DateTime pdFromDate, DateTime ldToDate, int piFormulaId, int piIsProcessedType, int piIsSeriesReStart, int piIsPLCalculate, int piIsIncludeEditedScoreint)
        {
            try
            {
                List<ResultData> myList = new List<ResultData>();
                sqlCommand.ExecuteNonQuery();
                dbModel.ResultData item = new dbModel.ResultData();
                item.Description = "succesfulll";
                item.Status = 1;
                item.BatchId = 100;
                myList.Add(item);
                return await Task.FromResult(myList);
            }
            catch (Exception ex)
            {
                Common.showMessageBox(ex.Message + "InnerException:" + ex.InnerException, MSG_TYP.Error, "Error Occured");
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM