I have a non Async method that needs to wait on the results of another method. That method itself builds an array of tasks, and waits on all of them, then returns a boolean result. I had to make the task that was called Async, and now I cannot get a simple boolean back out of it. When you read the code below, it all compiles except for the "StartNew ProcessDbList" line, it says it cannot convert a Task<bool> to a bool. How do I arrange this so that I get a simple boolean back that the ProcessInbox method can return?
public bool ProcessInbox(List<SessionContext> sessionContextList, FsFcsService curSvc)
{
bool resultBool = false;
List<SessionContext> tempSessionList = sessionContextList.ToList();
//below is the line that will not compile with the type conversion error
Task<bool> theTask = Task<bool>.Factory.StartNew(() => ProcessDbList(tempSessionList, curSvc));
resultBool = theTask.Result;
return resultBool;
}
public async Task<bool> ProcessDbList(List<SessionContext> sessionList, FsFcsService curSvc)
{
bool resultBool = false;
IEnumerable<Task<bool>> TasksList =
from SessionContext session in sessionList select ProcessDb(session, curSvc);
Task<bool>[] TaskArray = TasksList.ToArray();
// Await the completion of all the running tasks.
// this line is what forces the method to be Async and return a Task<bool>
bool[] resultsArr = await Task.WhenAll(TaskArray);
foreach (bool indivResult in resultsArr)
{
if (indivResult)
{
// if any result is true, this method returns true
resultBool = true;
break;
}
}
// this is where I should probably be returning something other than a simple bool
return resultBool;
}