Blazor .Net Task.WhenAll() 将会存货

huangapple go评论82阅读模式
英文:

Blazor .Net Task.Whenall() Will Stock

问题

Sure, here's the translated code:

伙计们。

我有一个问题,我使用Blazor来同步商城产品并跟踪同步任务
我创建了两个任务:

1. 任务 syncMallProductTask  --> 执行同步任务 <br>
**public async Task syncMallProductTask(args...)**

2. 任务 progressCheckTask  --> 长轮询跟踪进度 <br>
**public async Task<ActionResult<int>> GetProgress(string SyncGuid)**

然后使用 `await Task.WhenAll()` 来运行它们,然后在任务完成后执行某些操作。

但它会在 Task.WhenAll 上卡住,我尝试在这些函数中写日志,我确信它们已经完成了所有工作,我的日志是最后一行。

但是主任务在 Task.WhenAll() 之后还有一些事情要做,但它不会执行。

以下是主函数:

```csharp
public async Task SyncBtnClick()
{
    string SyncId = Guid.NewGuid().ToString();
    ProductSuccessCount = 0;
    GetItemListDto = await ProductService.GetItemList(MallId);
    ProductCount = GetItemListDto.Response.TotalCount;
    SyncState = false;
    await ModalSync.Show();

    Task syncMallProductTask = Task.Run(async () =>
    {
        await ProductService.SyncMallProduct(SyncId, MallId);
    });

    Task progressCheckTask = Task.Run(async () =>
    {
        while (ProductSuccessCount < ProductCount)
        {
            await Task.Delay(3000);
            ProductSuccessCount = await _http.GetFromJsonAsync<int>("api/Product/GetProgress?SyncGuid=" + SyncId);
            StateHasChanged();
        }
    });

    await Task.WhenAll(syncMallProductTask, progressCheckTask);   &lt;---- 会卡在这里    

    // 检查任务的完成情况
    if (syncMallProductTask.IsFaulted)
    {
        // 处理 syncMallProductTask 任务的异常
        Log.Warning("syncMallProductTask 任务出现异常: " + syncMallProductTask.Exception);
    }
    else if (progressCheckTask.IsFaulted)
    {
        // 处理 progressCheckTask 任务的异常
        Log.Warning("progressCheckTask 任务出现异常: " + progressCheckTask.Exception);
    }
    else
    {
        // 子任务成功完成后执行的操作
        Log.Information("子任务已完成");

        SyncState = true;
        
        await Task.Delay(3000); // 等待3秒,让数据库完全写入再读取
        await MallService.MallsSyncInitList();
        Mall = MallService.Malls.ToList();
        StateHasChanged();
    }
}

Hope this helps!

英文:

guys.

I have a question is I use Blazor to do sync mall products and track the sync task.
I created two Task

  1. Task syncMallProductTask --> do synctask <br>
    public async Task syncMallProductTask(args...)
  2. Task progressCheckTask --> to long pooling track progress <br>
    public async Task<ActionResult<int>> GetProgress(string SyncGuid)

Then use await Task.WhenAll() to run those, and do something after the Task finish.

But it will stock on Task.Whenall, I try to write log in those fuction's, I sure they have done all the work, my log is last line.

but the main Task have something to do after Task.WhenAll(), but it will not to do.

under is main function

public async Task SyncBtnClick()
{
string SyncId = Guid.NewGuid().ToString();
ProductSuccessCount = 0;
GetItemListDto = await ProductService.GetItemList(MallId);
ProductCount = GetItemListDto.Response.TotalCount;
SyncState=false;
await ModalSync.Show();
Task syncMallProductTask = Task.Run(async () =&gt;
{
await ProductService.SyncMallProduct(SyncId, MallId);
});
Task progressCheckTask = Task.Run(async () =&gt;
{
while (ProductSuccessCount &lt; ProductCount)
{
await Task.Delay(3000);
ProductSuccessCount = await _http.GetFromJsonAsync&lt;int&gt;(&quot;api/Product/GetProgress?SyncGuid=&quot; + SyncId);
StateHasChanged();
}
});
await Task.WhenAll(syncMallProductTask, progressCheckTask);   &lt;---- Will Stock at here    
// 检查任务的完成情况
if (syncMallProductTask.IsFaulted)
{
// 处理syncMallProductTask任务的异常
Log.Warning(&quot;syncMallProductTask任務出現異常: &quot; + syncMallProductTask.Exception);
}
else if (progressCheckTask.IsFaulted)
{
// 处理progressCheckTask任务的异常
Log.Warning(&quot;progressCheckTask任務出現異常: &quot; + progressCheckTask.Exception);
}
else
{
// 子任务成功完成后执行的操作
Log.Information(&quot;子任務已完成&quot;);
SyncState=true;
await Task.Delay(3000); //等待3秒,讓資料庫完全寫入再讀取
await MallService.MallsSyncInitList();
Mall = MallService.Malls.ToList();
StateHasChanged();
}
}

答案1

得分: 1

如果我理解正确,您可以简化SyncBtnClick。您不需要运行任务的部分。除非您运行处理器密集型代码,否则可以在UI线程上运行所有内容。

public async Task SyncBtnClick() 
{
    //...
    
    var syncMailTask = ProductService.SyncMallProduct(SyncId, MallId);
    var checkSuccessTask = CheckSuccessAsync();

    await Task.WhenAll(new Task[] { syncMailTask, checkSuccessTask});

    //... 剩下的部分


}

private async Task CheckSuccessAsync()
{
    while (ProductSuccessCount < ProductCount)
    {
        await Task.Delay(3000);
        ProductSuccessCount = await _http.GetFromJsonAsync<int>("api/Product/GetProgress?SyncGuid=" + SyncId);
        StateHasChanged();
    }
}

然而,要使此代码完成,需要CheckSuccessAsync完成。

英文:

If I'm reading this correctly, you can simplify your SyncBtnClick. You don't need the task run stuff. Unless you're running processor intensive code, you can run everything on the UI thread.

public async Task SyncBtnClick() 
{
    //...
    
    var syncMailTask = ProductService.SyncMallProduct(SyncId, MallId);
    var checkSuccessTask = CheckSuccessAsync();

    await Task.WhenAll(new Task[] { syncMailTask, checkSuccessTask});

    //... The rest


}

private async Task CheckSuccessAsync()
{
        while (ProductSuccessCount &lt; ProductCount)
        {
            await Task.Delay(3000);
            ProductSuccessCount = await _http.GetFromJsonAsync&lt;int&gt;(&quot;api/Product/GetProgress?SyncGuid=&quot; + SyncId);
            StateHasChanged();
        }
}

However, for this code to complete CheckSuccessAsync needs to complete.

huangapple
  • 本文由 发表于 2023年7月3日 02:13:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定