任务未从其他任务运行

Task not running from another task

提问人:Allan 提问时间:8/29/2023 最后编辑:Brian Tompsett - 汤莱恩Allan 更新时间:8/31/2023 访问量:72

问:

在 .Net 4.5 中 这种方法将任务添加到列表中。

//This method returns Datetime.
var task = Task.Factory.StartNew(() => ProcessPost(_blockSize), token);
tasks.Add(task);

我想运行另一个进程,但我的方法出错。ReturnString

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 执行 PostAsync,但该过程永远不会开始,也永远不会结束。

 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在另一个项目中工作,但不在这个项目中,有什么解决方案吗?

C# asp.net 任务 net-4.5

评论

2赞 Flydog57 8/29/2023
“但我的 ReturnString 方法有错误”是什么意思?它是否编译,是否抛出异常,是否执行(或不执行)意外操作?你打算不打电话吗?awaitvar baseTask = consultor.ReturnString(documento.number, documento.token);
0赞 Allan 8/29/2023
当进程运行 PostAsync 方法时,这不会执行,它会跳过该进程,并且永远不会启动它,即使它在 try catch 块中,也不会出现任何异常,只有在几分钟后才会出现“任务已取消”
5赞 Stephen Cleary 8/29/2023
您的代码正在将同步异步工作扔到线程池上(使用一个非常可疑的 API),并且似乎涉及某种工作队列。简而言之,有很多错误;你到底想做什么?
0赞 John Wu 8/30/2023
如果收到“任务已取消”,则 HTTP 请求似乎已超时。确保 URL 正确无误,并且可以使用普通浏览器访问它。

答:

0赞 Charlieface 8/30/2023 #1

您的代码可能处于死锁状态,因为您正在执行异步同步。相反,使用await

您还应该删除空块,以便正确传播错误,并且您有一个逻辑错误,无论您是否设法这样做,您都在递增。catchtotalToProcessPop

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;
}