等待在VB.NET中的异步函数

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

Waiting for an async function in vb.net

问题

以下是已经翻译好的部分:

"I'm trying out some async code to avoid locking up the UI while my program runs a time-consuming function (using Visual Studio 2022).

Here's what I've got so far - the program is running through pdf filename entries from a datagrid and performing the function on the filenames it finds:

Async Sub process_files()

    For Each myrow In DGV_inputfiles.Rows

        inputPDF = myrow.cells("col_filenamefull").value
        outputPDF = myrow.cells("col_outname").value

        Await Task.Run(Sub()
                           time_consuming_function(inputPDF, outputPDF)
                       End Sub)
    Next

End Sub

At the moment, the program is not waiting for the 'time_consuming_function' to finish, so it's getting to the end of the sub before some of the output files are generated - so it appears to the user that it has finished when it's actually still working.

I believe the solution is something to do with returning a value from the function and waiting for it, but I can't quite see how it works - could anyone help please?"

英文:

I'm trying out some async code to avoid locking up the UI while my program runs a time-consuming function (using Visual Studio 2022).

Here's what I've got so far - the program is running through pdf filename entries from a datagrid and performing the function on the filenames it finds:

Async Sub process_files()

    For Each myrow In DGV_inputfiles.Rows

        inputPDF = myrow.cells("col_filenamefull").value
        outputPDF = myrow.cells("col_outname").value

        Await Task.Run(Sub()
                           time_consuming_function(inputPDF, outputPDF)
                       End Sub)
    Next

End Sub

At the moment, the program is not waiting for the 'time_consuming_function' to finish, so it's getting to the end of the sub before some of the output files are generated - so it appears to the user that it has finished when it's actually still working.

I believe the solution is something to do with returning a value from the function and waiting for it, but I can't quite see how it works - could anyone help please?

答案1

得分: 0

time_consuming_function(...)中,您可以使用invoke向UI发送一些信息,就像这样(假设UI表单中存在textbox1):

Sub time_consuming_function(...)
    ' 进行你的操作...
    Me.BeginInvoke(Sub() textbox1.Text = "运行中...")
    ' 其他操作...
End Sub
英文:

in time_consuming_function(...) you can send some infos to UI using invoke like this (assuming textbox1 exists in UI form):

sub time_consuming_function(...)
.... your stuff....
Me.BeginInvoke(Sub() textbox1.Text = "running...")
....
end sub

答案2

得分: 0

Await 的效果是它将控制权返回给用户界面,直到被 Await 的表达式或调用完成。它似乎相当适合您的工作流程,您只需要对 process_files 进行一些更用户友好的更改。

例如,您可以在 UI 中更新正在处理的文件,然后在 Task.Run 之前的那一行进行更改。

例如:

'(在循环体内)
CurrentOperation = $"正在处理 {inputPdf} 到 {outputPdf}..."
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(CurrentOperation)))
Await Task.Run(...)

您可以在 For 循环之前禁用 UI 控件,在完成后重新启用它们。

Await 的好处是这些更改会很容易进行,而且例程的逻辑流程会很容易理解。

请注意,任何 Await 都提供了可重入代码的选项,因为用户可能会与 UI 进行交互(即使在所有操作都在一个线程上运行的情况下,例如异步的网络或 I/O 操作)。

如果您还没有这样做,我建议阅读 Stephen Cleary 关于 .NET 中异步操作的所有内容。

英文:

The effect of Await is that it returns control to the UI until the expression or call that is Awaited completes. It seems to be suited reasonably well to your workflow, you just need to make changes to process_files to be more user-friendly.

For example, you could have something in the UI update with the file that is currently being processed, and change it at the line before Task.Run.

e.g.

'(Inside the loop body)
CurrentOperation = $"Processing {inputPdf} into {outputPdf}..."
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(CurrentOperation)))
Await Task.Run(...)

You could disable UI controls before the For loop and re-enable them when it finishes.

The benefit of Await is that these changes will be easy, and the logical flow of the routine will be easy to follow.

Be aware that any Await presents an option for re-entrant code as the user may interact with the UI (this is true even for cases where everything is running on one thread as with async internet or I/O operations).

If you haven't done so already, I would recommend to read everything Stephen Cleary has written about asynchronous operations in .NET.

huangapple
  • 本文由 发表于 2023年2月8日 17:54:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384011.html
匿名

发表评论

匿名网友

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

确定