dcsimg
Results 1 to 7 of 7

Thread: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    Additional information: The process cannot access the file 'C:\Users\ACER\Desktop\Common-Mistakes-Backup.rtf' because it is being used by another process.

    The error appears when I open a file that has been saved or updated, when running the program at the same time. I want users to be able to look at their files whenever they want without the need of closing the program first after it is installed. The problem appear when I try Save or when I try to use Autosave Function that acts as a backup incase of power failure. Here is the code with the problem:

    Code:
            ElseIf File.Exists(path) = True Then
    
                ' Save the contents of the RichTextBox into the file.
                RichTextBox1.SaveFile(path, _
                    RichTextBoxStreamType.RichText)
    
                Timer1.Start()
            End If
    Here is the rest of the code:

    Code:
        Private Sub SaveRecord()
    
            'Declare a SaveFileDialog object
            Dim objSaveFileDialog As New SaveFileDialog
    
            'Set the Save dialog properties
            With objSaveFileDialog
                .DefaultExt = ".rtf"
                .FileName = "Common-Mistakes"
                .Filter = "Word Document (*.rtf)|*.rtf|(*.txt)|*.txt|All files (*.*)|*.*"
                .FilterIndex = 1
                .OverwritePrompt = True
                .Title = "SaveAs File Dialog"
            End With
    
            Dim path As String = (My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Common-Mistakes.rtf")
    
            ' This text is added only once to the file.
            If File.Exists(path) = False Then
    
                If objSaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Try
                        Dim filePath As String = System.IO.Path.Combine( _
                            My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                            objSaveFileDialog.FileName)
    
                        ' Save the contents of the RichTextBox into the file.
                        RichTextBox1.SaveFile(filePath, _
                            RichTextBoxStreamType.RichText)
    
                    Catch fileException As Exception
                        Throw fileException
                    End Try
                End If
    
            ElseIf File.Exists(path) = True Then
    
                ' Save the contents of the RichTextBox into the file.
                RichTextBox1.SaveFile(path, _
                    RichTextBoxStreamType.RichText)
                MessageBox.Show("You have already saved on that location!", "File Already Saved!", MessageBoxButtons.OK)
    
            End If
    
            'Clean up
            objSaveFileDialog.Dispose()
            objSaveFileDialog = Nothing
        End Sub
        Private Sub AutosaveFile()
    
            Dim path As String = (My.Computer.FileSystem.SpecialDirectories.Desktop & "\Common-Mistakes-Backup.rtf")
    
            ' This text is added only once to the file.
            If File.Exists(path) = False Then
    
                          ' Save the contents of the RichTextBox into the file.
                RichTextBox1.SaveFile(path, _
                    RichTextBoxStreamType.RichText)
    
            ElseIf File.Exists(path) = True Then
    
                ' Save the contents of the RichTextBox into the file.
                RichTextBox1.SaveFile(path, _
                    RichTextBoxStreamType.RichText)
    
                Timer1.Start()
            End If
        End Sub
        'End of Save Functions Sections.'

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    That error means some other program has opened the file and placed a 'lock' on it. This is typically done when that other program wants to write to the file. Locks are important, because if two programs write to a file at the same time it is very easy to corrupt the file.

    It sounds like /your/ program might be the one holding the lock, though. I looked at your code, and while there's a few things that raised eyebrows they wouldn't cause this. But your Autosave code runs in response to a timer, right? That brings up an important question: which Timer class are you using?

    Two of the Timer classes are designed to raise their event on the UI thread, where it's always safe to interact with controls.

    One of the Timer classes calls a Delegate on a worker thread. From that worker thread, it's never safe to interact with controls. And you interact with a control when you call RichTextBox1.SaveFile(). What happens when you do? Eldritch horrors spawn forth into sensitive Windows internal data structures. Sometimes you're lucky and the program crashes. More often, strange behaviors no one can reproduce occur. Given that, if you're calling that method on a worker thread, it's not entirely surprising that the AutoSave accidentally holds a lock forever.

    If that's not it, I'm stumped.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    AutoSave Function() Timer is for saving the RichtextBox content on the Private Sub RichTextBox1_TextChanged(). This can be done per second depending on the changes you make on the richtextbox! I did this to make the function as a backup just incase the power goes out. I need to open the file at the same time when the program is running because, that is what the users will be doing, and I don't want them to get errors like these because the program was not closed when they opened the document. they will get confused.
    Last edited by nqioweryuadfge; Jul 14th, 2016 at 10:13 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    I forgot to add this code:


    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            AutosaveFile()
        End Sub
        Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs)
    
            Timer1.Start()
    
        End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    Any solution?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2015
    Posts
    114

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    Any solutions please?

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    I think you are trying to save the file too frequently, and it's likely when a 2nd save operation starts the first hasn't finished. Slow your roll a bit. Instead of saving every keypress, try saving every 10-15 seconds instead.

    There's still a small chance you might try to save the moment the autosave happens. You should take care to make sure only one save operation happens at roughly the same time.

    If either of those doesn't work, I feel like there's probably more happening with this file, and other things reading/writing, but the solution will be the same no matter what: if two things can decide to try and save the file at the same time, the 2nd one is at risk for failure. So it's best to make sure you don't do that.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width