locked
Exit out of Application.DoEvents() loop in vb.net

    Question

  • Hi there ,
                  Can anyone tell me how to exit out of the Application.Do() as i am hanging in that .Here is a sample of my code.

    Imports PDFCreator
    Imports System.Windows.Forms
    Public Class Form1
        Private WithEvents _PDFCreator As PDFCreator.clsPDFCreator
        Dim ReadyState As Boolean
        Private Const maxTime As Long = 10
      
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim this_Directory As String = "C:\Documents and Settings\nsravanthi\Desktop"
            Dim PDFdestination_Directory As String = "C:\Documents and Settings\nsravanthi\Desktop"
            Dim CutOffDate As Date = "11/23/2009"
            _PDFCreator = New PDFCreator.clsPDFCreator

            Try
                Dim targetDirectory As String = this_Directory & "\"
                Dim fileEntry As String() = Directory.GetFiles(targetDirectory, "*.dwg")
                Dim fileName As String
                For Each fileName In fileEntry
                    Dim PDFconversionError As Integer = 0
                    Dim this_PDFfileName As String = ""
                    Dim fileNameCreationDate As String = File.GetCreationTime(fileName)
                    Dim fileNameModifiedDate As String = File.GetLastWriteTime(fileName)
                    If fileNameModifiedDate < CutOffDate Then
                        rtb_Text.AppendText("       ** " & fileName & " ... Modified: " & fileNameModifiedDate & vbCrLf)
                        'Log("       ** " & fileName & " ... Modified: " & fileNameCreationDate)

                        Dim FileAttribute As String = File.GetAttributes(fileName)
                        If (FileAttribute = FileAttributes.ReadOnly Or FileAttribute = 33) Then
                            'MsgBox(fileName & " is ReadOnly!")
                            'do NOT Call the PDF generator
                            rtb_Text.AppendText("           ** File Locked - cannot generate PDF." & vbCrLf)
                            '   Log("           ** File Locked - cannot generate PDF.")
                        Else
                            'Call the PDF generator
                            Dim arr(2) As Short
                            arr(0) = 0
                            arr(1) = 1
                            arr(2) = 2

                            'get length of the existing directory
                            'rename the extension to pdf
                            'select onlt the file name - no dirctory info
                            'add the PDFdestination_Directory and file name to set the output file name
                            Dim directory_length As Integer = Len(this_Directory) + 1
                            this_PDFfileName = Mid(fileName, 1, Len(fileName) - 3) + "pdf"
                            this_PDFfileName = Mid(this_PDFfileName, directory_length, 100)
                            this_PDFfileName = PDFdestination_Directory & this_PDFfileName

                            Dim strPDF As String = Mid(fileName, 1, Len(fileName) - 3) + "pdf"
                            Dim strDWG As String = fileName
                            rtb_Text.AppendText("            PDF Conversion Started for : " & this_PDFfileName & vbCrLf)


                            'Call the PDF generator
                            PDFconversionError = 0
                            Try
                                Dim opt As New PDFCreator.clsPDFCreatorOptions
                                opt.RunProgramAfterSaving = 1
                                opt.PrinterStop = 1
                                opt.NoPSCheck = 1
                                opt.NoProcessingAtStartup = 1
                                opt.DisableEmail = 1

                                opt = _PDFCreator.cOptions

                                With opt
                                    .UseAutosave = 1
                                    .UseAutosaveDirectory = 1
                                    .AutosaveDirectory = PDFdestination_Directory
                                    .AutosaveFormat = 0
                                    If .AutosaveFormat = 5 Then
                                        .BMPResolution = 72
                                    End If
                                    opt.AutosaveFilename = this_PDFfileName
                                End With
                                With _PDFCreator
                                    .cOptions = opt
                                    .cClearCache()
                                    .cDefaultPrinter = "PDFCreator"
                                    .cPrintFile(this_PDFfileName)
                                    .cPrinterStop = False
                                End With

                                ReadyState = False

                                With Timer1
                                    .Interval = maxTime * 1000
                                    .Enabled = True
                                    Do While Not ReadyState And .Enabled
                                    Application.DoEvents()------------------------------Hanging here----------------------------------------------
                                    Loop

                                    .Enabled = False
                                End With
                                If Not ReadyState Then
                                    MsgBox("Creating printer test page as pdf." & vbCrLf & vbCrLf & _
                                     "An error is occured: Time is up!", MsgBoxStyle.Exclamation, Me.Text)
                                End If
                                _PDFCreator.cPrinterStop = True
                                _PDFCreator.cDefaultPrinter = "PDFCreator"
                                opt = Nothing

                            Catch ex As Exception
                                MsgBox("Error Calling PDF converter " & ex.Message & ex.StackTrace & ex.Source)
                            End Try


                            Try
                                'oPDFmaker.CloseAcrobat()
                            Catch ex As Exception
                                PDFconversionError = 1
                                rtb_Text.AppendText("            PDF Conversion FAILED for : " & this_PDFfileName & vbCrLf)
                                MsgBox("Error Converting to PDF file " & ex.Message & ex.StackTrace & ex.Source)
                            End Try
                            If PDFconversionError = 0 Then
                                rtb_Text.AppendText("            PDF Conversion Completed for : " & this_PDFfileName & vbCrLf)
                            End If
                            ' Log("           ** PDF Generated.")
                        End If

                    Else
                        rtb_Text.AppendText("          " & fileName & " ... Modified: " & fileNameModifiedDate & vbCrLf)
                        'Log("          " & fileName & " ... Modified: " & fileNameCreationDate)
                    End If
                    rtb_Text.Refresh()
                Next fileName
            Catch ex As Exception
                MsgBox("ERROR: Available Directories " & ex.Message)
            End Try

        End Sub


    End Class
    Tuesday, December 15, 2009 9:44 PM

Answers

  • If that is the code that is just above your loop, then it's not actually executing while the loop is running.   But in any case there's nothing in that process that changes the value of ReadyState or the enabled state of Timer1, and the loop is not going to exit until one or both of those changes occur.

    What are you actually trying to achieve by putting this loop in the middle of your code?
    Tuesday, December 15, 2009 10:17 PM

All replies

  • What is the process that is going to change the value of ReadyState, or is going to change the enabled state of Timer1?  One of both of those things must occur for the loop to be exited.
    Tuesday, December 15, 2009 9:50 PM
  • This process should complete and the timer should exit the loop
     With _PDFCreator
                                    .cOptions = opt
                                    .cClearCache()
                                    .cDefaultPrinter = "PDFCreator"
                                    .cPrintFile(this_PDFfileName)
                                    .cPrinterStop = False
                                End With



    can you suggest me some change i might need to do inorder for this to work ?
    Tuesday, December 15, 2009 9:56 PM
  • If that is the code that is just above your loop, then it's not actually executing while the loop is running.   But in any case there's nothing in that process that changes the value of ReadyState or the enabled state of Timer1, and the loop is not going to exit until one or both of those changes occur.

    What are you actually trying to achieve by putting this loop in the middle of your code?
    Tuesday, December 15, 2009 10:17 PM