In .Net 4.5 This methos add a task into a list.
//This method returns Datetime.
var task = Task.Factory.StartNew(() => ProcessPost(_blockSize), token);
tasks.Add(task);
And I want to run another process, but I have an error with ReturnString Method.
public virtual DateTime ProcessPost(int blockSize)
{
Interlocked.Add(ref totalToProcess, blockSize);
DateTime finishTask;
try
{
for (int i = 0; i < blockSize; i++)
{
try
{
bool isSuccess = stackProcesos.TryPop(out documento);
if (isSuccess)
{
// Verifies if document exist.
if (!HelperString.IsNullOrEmpty(documento.ruta) && HelperFile.ExistsFile(documento.ruta)){}
else
{
//In this part ocurrs an error.
if (!HelperString.IsNullOrEmpty(documento.number))
{
ConsultorSTR consultor = new ConsultorSTR();
var baseTask = consultor.ReturnString(documento.number, documento.token);
//no execution I don't know why
baseTask.Wait();
//other things
}
};
}
}
catch (Exception ex)
{}
}
}
catch (Exception e)
{}
}
ReturnString do a PostAsync, but the process never start and never ends.
public async Task<string> ReturnString(string Number, string token)
{
var XmlRequestContent = new StringContent($"foo-content", Encoding.UTF8, "text/xml");
//Response never comes
var response = await client.PostAsync("url/api/returnData", XmlRequestContent);
return "";
}
ReturnString works in another project, but not in this, any solution?
Your code is probably deadlocking, because you are doing sync-over-async. Instead make this code fully async using
awaitYou also should remove the empty
catchblocks so that errors get propogated correctly, and you have a logic error where you are incrementingtotalToProcessregardless whether you managed toPopor not.