签名异步。和同步方法
Signature async. and synchronous methods
如果我有一个方法 Sub Foo(fooParam as String)
并且我想编写像
这样的异步版本
Function FooAsync (fooParam as String) as Task
Return Task.Run(Sub() Foo(fooParam))
End Function
现在需要进度和注销,所以需要把签名改成
Function FooAsync (fooParam as String, progress as IProgress(of String), cancellationToken as CancellationToken) as Task
Return Task.Run(Sub() Foo(fooParam))
End Function
Microsoft 说异步版本应该与同步版本具有相同的签名,那么这里的最佳实践是什么,还是我理解错了?
将 progress 和 cancellationToken 传递给同步。方法对我来说似乎毫无意义......但只有这样我才能结束同步。方法和 return 它作为任务。
为了更好地理解这里的代码,了解我现在的表现。在 Async 中,刚刚编写了添加进度和取消调用的同步方法。但我确定这不是我应该这样做的方式:
Public Sub DoCalibration(calibrationHoldTimeForReading As Integer)
If Not PressureCalibrator.SerialPort.IsOpen() Then
PressureCalibrator.SerialPort.Open()
End If
PressureCalibrator.PressureUnit = PressureUnit.bar
For Each point In InputList
SetMessPointAndMeasure(point, calibrationHoldTimeForReading)
Next
PressureCalibrator.Vent()
PressureCalibrator.SerialPort.Close()
Log.WriteLog("Calibration completed.")
End Sub
Public Function DoCalibrationAsync(calibrationHoldTimeForReading As Integer, Optional progress As IProgress(Of CalibrationStepResult) = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task
Return Task.Run((Sub()
If Not PressureCalibrator.SerialPort.IsOpen() Then
PressureCalibrator.SerialPort.Open()
End If
PressureCalibrator.PressureUnit = PressureUnit.bar
For Each point In InputList
Dim measureResult = SetMessPointAndMeasure(point, calibrationHoldTimeForReading)
If Not IsNothing(cancellationToken) Then
CancellationToken.ThrowIfCancellationRequested()
End If
If Not IsNothing(progress) Then
progress.Report(measureResult)
End If
Next
PressureCalibrator.Vent()
PressureCalibrator.SerialPort.Close()
Log.WriteLog("Calibration completed.")
End Sub), cancellationToken)
End Function
感谢@PauloMorgado,我能够通过阅读他发布的文章以及其他相关文章来理解此处的指南。
如果我有一个方法 Sub Foo(fooParam as String)
并且我想编写像
Function FooAsync (fooParam as String) as Task
Return Task.Run(Sub() Foo(fooParam))
End Function
现在需要进度和注销,所以需要把签名改成
Function FooAsync (fooParam as String, progress as IProgress(of String), cancellationToken as CancellationToken) as Task
Return Task.Run(Sub() Foo(fooParam))
End Function
Microsoft 说异步版本应该与同步版本具有相同的签名,那么这里的最佳实践是什么,还是我理解错了?
将 progress 和 cancellationToken 传递给同步。方法对我来说似乎毫无意义......但只有这样我才能结束同步。方法和 return 它作为任务。
为了更好地理解这里的代码,了解我现在的表现。在 Async 中,刚刚编写了添加进度和取消调用的同步方法。但我确定这不是我应该这样做的方式:
Public Sub DoCalibration(calibrationHoldTimeForReading As Integer)
If Not PressureCalibrator.SerialPort.IsOpen() Then
PressureCalibrator.SerialPort.Open()
End If
PressureCalibrator.PressureUnit = PressureUnit.bar
For Each point In InputList
SetMessPointAndMeasure(point, calibrationHoldTimeForReading)
Next
PressureCalibrator.Vent()
PressureCalibrator.SerialPort.Close()
Log.WriteLog("Calibration completed.")
End Sub
Public Function DoCalibrationAsync(calibrationHoldTimeForReading As Integer, Optional progress As IProgress(Of CalibrationStepResult) = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task
Return Task.Run((Sub()
If Not PressureCalibrator.SerialPort.IsOpen() Then
PressureCalibrator.SerialPort.Open()
End If
PressureCalibrator.PressureUnit = PressureUnit.bar
For Each point In InputList
Dim measureResult = SetMessPointAndMeasure(point, calibrationHoldTimeForReading)
If Not IsNothing(cancellationToken) Then
CancellationToken.ThrowIfCancellationRequested()
End If
If Not IsNothing(progress) Then
progress.Report(measureResult)
End If
Next
PressureCalibrator.Vent()
PressureCalibrator.SerialPort.Close()
Log.WriteLog("Calibration completed.")
End Sub), cancellationToken)
End Function
感谢@PauloMorgado,我能够通过阅读他发布的文章以及其他相关文章来理解此处的指南。