Task not running from another task

75 Views Asked by At

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?

1

There are 1 best solutions below

0
Charlieface On

Your code is probably deadlocking, because you are doing sync-over-async. Instead make this code fully async using await

You also should remove the empty catch blocks so that errors get propogated correctly, and you have a logic error where you are incrementing totalToProcess regardless whether you managed to Pop or not.

public virtual async Task<DateTime> ProcessPost(int blockSize)
{
    for (int i = 0; i < blockSize; i++)
    {
        bool isSuccess = stackProcesos.TryPop(out documento);
        if (!isSuccess)
            break;

        Interlocked.Increment(ref totalToProcess);
        // Verifies if document exist.
        if (!HelperString.IsNullOrEmpty(documento.ruta) && HelperFile.ExistsFile(documento.ruta))
            continue;

        if (!HelperString.IsNullOrEmpty(documento.number))
        {
            ConsultorSTR consultor = new ConsultorSTR();
            await consultor.ReturnString(documento.number, documento.token);
                                    
                //other things
        }
    }
    return someValueHere;
}