我需要使用调用异步方法时调用Task.Run?

  • 本文关键字:调用 Task Run 异步方法 vb.net
  • 更新时间 :
  • 英文 :


我有一个来自DLL的事件之一的问题,我决定将一些代码移动到一个单独的Sub并使该调用异步(具有延迟):

Public Async Sub DeskoScan()

ListAdd("Perform scan START.")
Try
If _Duplex AndAlso _firstImageFileUploaded Then
Await Task.Delay(1000) ' here I want to be sure that all things related to the previous scanning process are closed
Dim Aborted As Boolean
Using frm = New countingForm
frm.lblTime.Text = _My_Settings.Item("WaitForDialog").Value
frm.lblMessage.Text = "Flip Document For Second Page Scan" _
+ vbCrLf + " - press OK when ready, or wait for count down to 0" _
+ vbCrLf + " - press Retry for another " + _My_Settings.Item("WaitForDialog").Value + " seconds" _
+ vbCrLf + " - press Abort to just cancel it"
frm.btnRetry.DialogResult = DialogResult.None
Aborted = frm.ShowDialog(Me) = DialogResult.Abort
End Using
If Aborted Then
Exit Sub
End If
End If
_deviceThread.Execute(Sub(ByVal device As Desko.Scan.Device)
device.Snapshot.Reset()
device.Snapshot.Infrared.Enabled = False
device.Snapshot.Infrared.Flags = Desko.Scan.ScanFlags.None
device.Snapshot.Infrared.ShutterWidthFactor = 1
device.Snapshot.Visible.Enabled = True
device.Snapshot.Visible.Flags = Desko.Scan.ScanFlags.DocumentClipping Or Desko.Scan.ScanFlags.AmbientFilter
device.Snapshot.Visible.ShutterWidthFactor = 1
device.Snapshot.Ultraviolet.Enabled = False
device.Snapshot.Ultraviolet.Flags = Desko.Scan.ScanFlags.None
device.Snapshot.Ultraviolet.ShutterWidthFactor = 1
device.Snapshot.Resolution = Desko.Scan.Resolution.Full
device.Snapshot.Perform()
End Sub)
Catch ex As Exception
ListAdd(ex.ToString())
End Try
End Sub

因为这个过程是在与主线程分开的一个单独的线程中运行的,所以我这样称呼它:

If Me.InvokeRequired Then
Me.Invoke(Sub() DeskoScan())
Else
DeskoScan()
End If

现在,在那个事件中,我想调用那个代码但是要进行异步调用,像这样:

Private Sub HandleSnapshotCompleted(ByVal sender As Object, ByVal args As Desko.Scan.SnapshotCompletedEventArgs)
If _Duplex AndAlso _firstImageFileUploaded Then
Task.Run(Sub() DeskoScan()) 'Is this enough or do I need Me.Invoke too?
Else
ListAdd("Snapshot completed. You may remove the document.")
Minimize()
End If
End Sub

我的问题是我需要在这里调用调用或任务。跑就够了?(我不能测试这段代码,因为我缺少一个扫描仪)

是的,Craig,你是对的,我不需要任务。在这里运行,但我需要async await Sub,这样我就可以确保扫描仪对象上的所有事件处理程序都被处理。通过执行Await Task.Delay(2000),我将使子线程异步化。

Public Async Sub DeskoScan()
If Me.InvokeRequired Then
Me.Invoke(Sub() DeskoScan())
Else
Await Task.Delay(2000)
....
End If
End Sub

ASWAG因为不清楚你在做什么……

Private Async Sub DeskoScanTsk()
Dim tsk As Task
tsk = Task.Run(Sub() DeskoScan())
Await tsk
End Sub
Private Sub HandleSnapshotCompleted(ByVal sender As Object, ByVal args As Desko.Scan.SnapshotCompletedEventArgs)
If _Duplex AndAlso _firstImageFileUploaded Then
DeskoScanTsk()
Else
ListAdd("Snapshot completed. You may remove the document.")
Minimize()
End If
End Sub
Public Sub DeskoScan()
If Me.InvokeRequired Then
Me.Invoke(Sub() DeskoScan())
Else
ListAdd("Perform scan START.")
Try
If _Duplex AndAlso _firstImageFileUploaded Then
Dim Aborted As Boolean
Using frm = New countingForm
frm.lblTime.Text = _My_Settings.Item("WaitForDialog").Value
frm.lblMessage.Text = "Flip Document For Second Page Scan" _
+ vbCrLf + " - press OK when ready, or wait for count down to 0" _
+ vbCrLf + " - press Retry for another " + _My_Settings.Item("WaitForDialog").Value + " seconds" _
+ vbCrLf + " - press Abort to just cancel it"
frm.btnRetry.DialogResult = DialogResult.None
Aborted = frm.ShowDialog(Me) = DialogResult.Abort
End Using
If Aborted Then
Exit Sub
End If
End If
_deviceThread.Execute(Sub(device As Desko.Scan.Device)
device.Snapshot.Reset()
device.Snapshot.Infrared.Enabled = False
device.Snapshot.Infrared.Flags = Desko.Scan.ScanFlags.None
device.Snapshot.Infrared.ShutterWidthFactor = 1
device.Snapshot.Visible.Enabled = True
device.Snapshot.Visible.Flags = Desko.Scan.ScanFlags.DocumentClipping Or Desko.Scan.ScanFlags.AmbientFilter
device.Snapshot.Visible.ShutterWidthFactor = 1
device.Snapshot.Ultraviolet.Enabled = False
device.Snapshot.Ultraviolet.Flags = Desko.Scan.ScanFlags.None
device.Snapshot.Ultraviolet.ShutterWidthFactor = 1
device.Snapshot.Resolution = Desko.Scan.Resolution.Full
device.Snapshot.Perform()
End Sub)
Catch ex As Exception
ListAdd(ex.ToString())
End Try
End If
End Sub

最新更新