英文:
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); <---- 会卡在这里
// 检查任务的完成情况
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
- Task syncMallProductTask --> do synctask <br>
public async Task syncMallProductTask(args...) - 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 () =>
{
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); <---- Will Stock at here
// 检查任务的完成情况
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();
}
}
答案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 < ProductCount)
{
await Task.Delay(3000);
ProductSuccessCount = await _http.GetFromJsonAsync<int>("api/Product/GetProgress?SyncGuid=" + SyncId);
StateHasChanged();
}
}
However, for this code to complete CheckSuccessAsync
needs to complete.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论