1

我有一个项目,我使用后台工作人员通过 WIA2 从扫描仪异步执行扫描功能。它适用于 WIA。

现在我正在尝试通过使用 TWAIN 扫描来做同样的事情。我可以使用 TWAIN 进行扫描。但是,当我尝试使其在后台工作时,我无法使其正常工作,因为在 TWAIN 中进行扫描的事件有一个 TransferImage 处理程序和一个 ScanningComplete 事件处理程序,它们会在扫描完成时被激发。transferImage 没问题,因为它不会影响我的后台事件。但是我想在scanningComplete事件中访问一个面板,让它.Visible = False

发生的事情的一部分:

Private Sub rBEScan_Click(sender As Object, e As EventArgs) Handles rBEScan.Click
    rPScanning.Visible = True
    Me.rBEScan.Enabled = False
    bGWScan.RunWorkerAsync()
End Sub
Private Sub bGWScan_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bGWScan.DoWork
    Dim path As String = ""
    Dim correct As Boolean = False
    If scanMode = 1 Then
        correct = ScannerRead(path, 1)
    Else 'TWAIN
        images = Nothing
        images = New List(Of System.Drawing.Bitmap)
        correct = scanTWAIN(gLocalScanner, path)
    End If
    Dim obj As Object
    obj = correct
    e.Result = obj
End Sub
Private Sub bGWScan_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) _
    Handles bGWScan.RunWorkerCompleted
    Dim obj As Object
    obj = e.Result
    Dim lobj As Boolean
    lobj = DirectCast(obj, Boolean)
    rPScanning.Visible = False
    Me.rBEScannerEskaneatu.Enabled = True
End Sub
Private Shared Function scanTWAIN(ByVal id As String, ByVal path As String) As Boolean ' prompt to scan more pages, 
' SCAN TWAIN FUNCTION
AddHandler twain.TransferImage,
        Sub(sender As Object, args As TwainDotNet.TransferImageEventArgs)
            If (Not (args.Image Is Nothing)) Then
                images.Add(args.Image)
            End If
        End Sub

    ' Re-enable the form after scanning completes
    AddHandler twain.ScanningComplete,
        Sub(sender As Object, e As TwainDotNet.ScanningCompleteEventArgs)
            'Enabled = True
            Dim lobj As String = ""
            lobj = FuncionScanner.pdfIrudiraTwain(images, path, gLocalPreguntarRotacion, orritxurisep, orriDok, orriguztiakDok)
            PrincipalR.rPScanning.Visible = False
            PrincipalR.rBEScan.Enabled = True
        End Sub
   ScanningFunctionOfTwain With my settings.
End Sub

我的问题是我在扫描图像之前结束了后台工作人员,因为事件是在另一个异步调用的函数中处理和触发的。

关于如何在后台工作人员中结束事件的任何rPScanning.Visible = False 想法rBEScan.Visible = False

如果不可能,我应该在 TWAIN 的扫描页面的 eventHandler 中使用另一个后台工作程序。

4

1 回答 1

1

感谢@JQSOFT,正如您所说,我必须使用委托子来处理面板可见性。这是代码中的更改:

Private Shared Function scanTWAIN(ByVal id As String, ByVal path As String) As Boolean ' prompt to scan more pages, 
' SCAN TWAIN FUNCTION
    AddHandler twain.TransferImage,
        Sub(sender As Object, args As TwainDotNet.TransferImageEventArgs)
            If (Not (args.Image Is Nothing)) Then
                images.Add(args.Image)
            End If
        End Sub

    ' Re-enable the form after scanning completes
    AddHandler twain.ScanningComplete,
        Sub(sender As Object, e As TwainDotNet.ScanningCompleteEventArgs)
            Dim objNewThread As New Thread(Sub() FuncionScanner.pdfTwainThread(images, path, gLocalPreguntarRotacion,
                                                                                   orritxurisep, orriDok, orriguztiakDok,
                                                                                   rPScanning, rBEScan))
            objNewThread.IsBackground = True
            objNewThread.Start()
        End Sub
   ScanningFunctionOfTwain With my settings.
End Sub

我已经把它放在一个新的线程后扫描功能中,效果很好。因为我在另一个类中使用了新线程,所以我也放置了新的委托子。

    Public Shared Sub pdfTwainThread(ByVal lista As List(Of System.Drawing.Bitmap),
                                     ByVal path As String, ByVal ori As Boolean,
                                     ByVal orritxurisep As Boolean, ByVal orriDok As Boolean,
                                     ByVal orriguztiakDok As Boolean,
                                     ByRef rp As Telerik.WinControls.UI.RadPanel,
                                     ByRef rb As Telerik.WinControls.UI.RadButtonElement)
        Dim l As String = ""
        l = pdfIrudiraTwain(lista, path, ori, orritxurisep, orriDok, orriguztiakDok)
        panel_visible(False, rp, rb)
        Dim txt As String = ""
        Dim txt1 As String = ""
        txt1 = Func_nombre(366)
        If l <> "" Then
            txt = Func_nombre(436)
            MessageBox.Show(txt, txt1, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            txt = Func_nombre(437)
            MessageBox.Show(txt, txt1, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
    Delegate Sub PanelV(vf As Boolean, ByRef rp As Telerik.WinControls.UI.RadPanel,
                        ByRef rbscan As Telerik.WinControls.UI.RadButtonElement)
    Private Shared Sub panel_visible_hemen(ByVal vis As Boolean, ByRef rp As Telerik.WinControls.UI.RadPanel,
                                           ByRef rbscan As Telerik.WinControls.UI.RadButtonElement)
        If rp.InvokeRequired Then
            Dim d As New PanelV(AddressOf panel_visible_hemen)
            rp.Invoke(d, New Object() {vis, rp, rbscan})
        Else
            rp.SendToBack()
            rp.Visible = vis
            rbscan.Enabled = True
        End If
    End Sub

我将面板和 buttonElement byref 传递给我在委托子中更新的新函数。

非常感谢@JQSOFT 的帮助。

我已经发布了答案以防万一其他人需要。

于 2019-12-04T07:58:46.643 回答