locked
OpenFileDialog Hangs used once ...

    Question

  • I'm deploying an application to a windows 7 (x86). When I'm running my application in the target computer I'm able to use the OpenFileDialog once. The second time I tried  I got this message:

    ApplicationName has stopped working

    A problem caused the program to stop working correctly.

    Windows will close the program and notify you if a solution is available.

    When I run my application in my development machine I do not have this issue

    Here is my code:

      Private Sub OpenFile()

            Dim OpenFileDialog As New OpenFileDialog
            OpenFileDialog.InitializeLifetimeService()


            Dim FileName As String
            OpenFileDialog.AutoUpgradeEnabled = False
            OpenFileDialog.InitialDirectory = My.Computer.FileSystem.CurrentDirectory & "\Programs\"
            OpenFileDialog.Filter = "Selective Solder Files (*.accdb)|"

            If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
                '  OpenFileDialog.ShowDialog()
                Beep()
                FileName = OpenFileDialog.FileName
            Else

                MsgBox("Invalid File Name", MsgBoxStyle.Critical, "ABORT FILE OPEN")
                Exit Sub

            End If
            CurrentProgramName = OpenFileDialog.FileName
            OpenFileDialog.Dispose()
            frmMainMenu.Close()
            frmMainMenu.MdiParent = Me
            frmMainMenu.ProgramDatabaseLoad(OpenFileDialog.FileName)
            Me.UpdateDDMSSDatabase()

        End Sub

    Thanks


    Ros

    Saturday, May 26, 2012 4:49 PM

Answers

  • The problem was resolved by re-deploying the application to the target machine.

    Thanks for your input


    Ros

    • Marked as answer by rosvb Monday, May 28, 2012 3:22 PM
    Monday, May 28, 2012 3:22 PM

All replies

  • Do you really need OpenFileDialog.InitializeLifetimeService()? See the documentation for it: LifetimeServices Class.

    Also, you are calling .Dispose on the OpenFileDialog and then later referencing it in frmMainMenu.ProgramDatabaseLoad(OpenFileDialog.FileName). And it probably isn't a good idea to name a variable the same as the Class.

    --
    Andrew


    Saturday, May 26, 2012 5:37 PM
  • I don't really understand why you would want to say "Invalid File Name" when a user has pressed cancel, the openfiledialog will not proceed if the user inputs an invalid/non-existant filename. So basically messageboxing "invalid file name" when the user cancels, is not even accurately describing what just happened. Also Why beep when they get it right? Usually a beep indicates an error of some kind, and not a success!

    If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.

    Saturday, May 26, 2012 6:44 PM
  • Your filter string does not follow the required format.  It should probably be

      "Selective Solder Files (*.accdb)|*.accdb"

    http://msdn.microsoft.com/en-us/library/microsoft.win32.filedialog.filter.aspx

    Sunday, May 27, 2012 12:28 AM
  • I also want to say, that the way you're using the openfiledialog seems a bit strange to me, you have an "OpenFile" subroutine, yet you don't really have any file opening going on in that routine, instead, esentially you are selecting a file. I suggest taking a few steps back and looking at a reduced way of opening a file. I have provided such an example.

        Sub OpenFile()
            Dim OpenFileDialog As New OpenFileDialog
            OpenFileDialog.Filter = "Any File *.*|*.*"
            If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim FileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(OpenFileDialog.FileName)
                Dim FileText As String = My.Computer.FileSystem.ReadAllText(OpenFileDialog.FileName)
                '-------------------------
                'Load your file here!
                '------------------------
            End If
        End Sub


    If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.

    Sunday, May 27, 2012 1:34 AM
  • Thank you for your suggestions, but still my problem is avoiding getting this message:

    ApplicationName has stopped working

    A problem caused the program to stop working correctly.

    Windows will close the program and notify you if a solution is available.

    This message never happened when I running the application in my development computer.This message only pops up in the target computer, Never the first time I try to open a file using the OpenFileDialog, but the second time


    Ros

    Sunday, May 27, 2012 2:56 PM
  • Probably not, some other forum threads suggested, but it did not make a difference, I still get the following message:

    ApplicationName has stopped working

    A problem caused the program to stop working correctly.

    Windows will close the program and notify you if a solution is available.

    This message never happened when I running the application in my development computer.This message only pops up in the target computer, Never the first time I try to open a file using the OpenFileDialog, but the second time


    Ros

    Sunday, May 27, 2012 2:58 PM
  • At what point does the program fail? As the dialog is showing, or after a file has been chosen?

    --
    Andrew

    Sunday, May 27, 2012 4:57 PM
  • Can you show us your code as it appears with the suggestions that we have given you so far?

    If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.

    Sunday, May 27, 2012 7:43 PM
  • The problem was resolved by re-deploying the application to the target machine.

    Thanks for your input


    Ros

    • Marked as answer by rosvb Monday, May 28, 2012 3:22 PM
    Monday, May 28, 2012 3:22 PM
  • You may have avoided the problem, but it is certainly not solved.

    This code is wrong:

            OpenFileDialog.Dispose()
            frmMainMenu.Close()
            frmMainMenu.MdiParent = Me
            frmMainMenu.ProgramDatabaseLoad(OpenFileDialog.FileName)

    You have accessed the object after you have disposed of it.  In fact, you already have created and updated a variable (CurrentProgramName) that would enable you to avoid that illegal access, but you aren't using it.

    The problem will almost certainly re-appear, perhaps in a slightly different form, at some time in the future. 

    Monday, May 28, 2012 9:45 PM