Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hitting open from OpenFileDialog kills Background Worker

Status
Not open for further replies.

ProtocolPirate

Programmer
Nov 21, 2007
104
US
I tried both adding an OpenFileDialog to my form and creating it in my code, but either way when I hit the Open button my background worker thread immediately halts without executing the RunWorkerCompleted method.
 
I don't know enough about background worker threads. What I would suggest is try starting the code that uses the OpenFileDialog in a new thread.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Private Sub ViewerDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
BackgroundWorker_Scan.RunWorkerAsync()

Dim dlgOpen As System.Windows.Forms.OpenFileDialog = New System.Windows.Forms.OpenFileDialog
If m_bAppendFromFile Then
dlgOpen.Filter = "PDF|*.PDF"
dlgOpen.FileName = "*.pdf"
dlgOpen.ShowDialog()
If dlgOpen.ShowDialog() <> DialogResult.OK Then Return
End If

If Not m_bAppendFromFile Then Return

' ********* Never gets past this while loop because the
' ********* background worker never finishes to set the
' ********* boolean to false.

While m_bWorkerRunning
Thread.Sleep(500)
End While

AxGdViewer1.DisplayFromPdfFile(dlgOpen.FileName)

Catch except As Exception
MsgBox(MethodBase.GetCurrentMethod.Name & " " & except.Message, vbCritical)
End Try
End Sub



Private Sub BackgroundWorker_Scan_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker_Scan.DoWork
Try
m_bWorkerRunning = True

Dim nAcquiredImage As Long

m_strPath = "\\Umisrv\EMR\EOB Images\" & m_strInsCode & "\"
m_strFile = m_datePosted.Year & "-" & m_datePosted.Month & "-" & m_datePosted.Day & " " & m_strCheckNumber & ".pdf"

If m_bAppend Then
AxGdViewer1.DisplayFromPdfFile(m_strPath & m_strFile)
AxGdViewer1.DisplayFirstFrame()
m_nTiffMultipage = AxImaging1.TiffCreateMultiPageFromGdPictureImage(AxGdViewer1.GetNativeImage)
Do
Debug.Write("1")
AxGdViewer1.DisplayNextFrame()
Debug.Write("2")
AxImaging1.TiffAppendPageFromGdPictureImage(m_nTiffMultipage, AxGdViewer1.GetNativeImage)
Debug.Write("3")
BackgroundWorker_Scan.ReportProgress(AxGdViewer1.CurrentPage)
Debug.Write("4")
Loop While AxGdViewer1.CurrentPage < AxGdViewer1.PageCount
Debug.WriteLine("5")

If Not m_bAppendFromFile Then
retry: delegateWaitTillLoaded()
If AxImaging1.TwainGetState < GdPicturePro5S.TwainStatus.TWAIN_SM_OPEN Then Return
If Not AxImaging1.TwainIsFeederLoaded Then GoTo retry

While AxImaging1.CreateImageFromTwain <> 0
nAcquiredImage = AxImaging1.GetNativeImage
AxImaging1.ConvertTo8BppQ()
AxImaging1.TiffAppendPageFromGdPictureImage(m_nTiffMultipage, nAcquiredImage)
AxGdViewer1.SetNativeImage(m_nTiffMultipage)
AxGdViewer1.DisplayLastFrame()
AxImaging1.CloseImage(nAcquiredImage)
BackgroundWorker_Scan.ReportProgress(0)
End While

AxGdViewer1.DisplayFirstFrame()
Do
AxGdViewer1.DisplayNextFrame()
Loop While AxGdViewer1.CurrentPage < AxGdViewer1.PageCount
End If
Else
Dim nPageCount As Long = AxGdViewer1.PageCount
Dim dirInfo As DirectoryInfo = New DirectoryInfo(m_strPath)
If Not dirInfo.Exists Then
CreateDirectory(m_strPath)
End If

retry2: delegateWaitTillLoaded()
If AxImaging1.TwainGetState < GdPicturePro5S.TwainStatus.TWAIN_SM_OPEN Then Return
If Not AxImaging1.TwainIsFeederLoaded Then GoTo retry2

While AxImaging1.CreateImageFromTwain <> 0
nPageCount += 1
AxImaging1.ConvertTo8BppQ()
nAcquiredImage = AxImaging1.GetNativeImage
If nPageCount = 1 Then
m_nTiffMultipage = AxImaging1.TiffCreateMultiPageFromGdPictureImage(nAcquiredImage)
Else
AxImaging1.TiffAppendPageFromGdPictureImage(m_nTiffMultipage, nAcquiredImage)
End If
AxGdViewer1.SetNativeImage(m_nTiffMultipage)
AxGdViewer1.DisplayLastFrame()
AxImaging1.CloseImage(nAcquiredImage)
BackgroundWorker_Scan.ReportProgress(nPageCount)
End While
End If
Catch except As Exception
m_bWorkerRunning = False
MsgBox(MethodBase.GetCurrentMethod.Name & " " & except.Message, vbCritical)
End Try
End Sub
 
I haven't worked through your code thoroughly, I'm afraid I saw the GoTo's and pretty much lost interest! - couldn't you rewrite those as while loops?

However, as far as I can see, you only set m_bWorkerRunning to false in one place in the Catch block - so, unless there is an exception it will never become false, try adding it immediately above the Catch statement.


Hope this helps.

[vampire][bat]
 
No, it doesn't help, that was an artifact of massively over-editing for the sake of brevity. I changed the three line goto to a four line do while. Totally irrelevant.

The problem is the twain library. When I took out all the twain calls the background thread doesn't hang after the OpenFileDialog returns.
 
I didn't suggest that rewriting the GoTo statements would solve the problem, my suggestion was to do with the placement of the setting of m_bWorkerRunning

Your code:

Code:
...
...

                  AxImaging1.CloseImage(nAcquiredImage)
                  BackgroundWorker_Scan.ReportProgress(nPageCount)
                End While
            End If
        Catch except As Exception
            m_bWorkerRunning = False
            MsgBox(MethodBase.GetCurrentMethod.Name & " " & except.Message, vbCritical)
        End Try
...
...

my suggestion:

Code:
...
...

                  AxImaging1.CloseImage(nAcquiredImage)
                  BackgroundWorker_Scan.ReportProgress(nPageCount)
                End While
            End If
            [COLOR=red][B][I]m_bWorkerRunning = False[/I][/B][/color]
        Catch except As Exception
            m_bWorkerRunning = False
            MsgBox(MethodBase.GetCurrentMethod.Name & " " & except.Message, vbCritical)
        End Try
...
...


As your code stands, as far as I can see, you only set m_bWorkerRunning to false in the Catch block. As such, when the thread ends normally, it is still true. I've suggested that you explicitly set it to false when the thread has finished processing.

There may well be other problems associated with the TWAIN calls, but those would be separate issues.


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top