Reputation: 1009
I have a problem with one of the events from a DLL, and I've decided to move some code to a separate Sub and make that call async (with a delay):
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
And because the procedure is run in a separate thread apart from the main one, I call it like this:
If Me.InvokeRequired Then
Me.Invoke(Sub() DeskoScan())
Else
DeskoScan()
End If
Now in that event, I want to call that code but to make an async call, like this:
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
My question is do I need here Invoke call or Task.Run is enough? (I can't test this code because I am missing a scanner)
UPDATE
Yes Craig you are right I don't need Task.Run here, but I need async await Sub so I can make sure that all event handlers on scanner objects are processed. By doing Await Task.Delay(2000) I will make the Sub asynchronous.
Public Async Sub DeskoScan()
If Me.InvokeRequired Then
Me.Invoke(Sub() DeskoScan())
Else
Await Task.Delay(2000)
....
End If
End Sub
Upvotes: 0
Views: 142
Reputation: 11773
A SWAG because it is not clear what you are doing...
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
Upvotes: 1