Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Exception on background worker Do_Work"
"Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader."

I have a background worker on first page to load image from MySQL with datatype BLOB. Byte of image is big since I'm running the program on 1080x1920(Portait) orientation. If use touch, it will navigate to welcome page. But I'm having a problem when program is gather data from database and background is CancelAsync where on next query, the DataReader is not close yet since it still query and CancelAsync is called. And exception for I/O race also happen. Below are my log events where it happen:-

HTML
5/28/2015 12:09:36 PM - ERRO - Error get advertisement image.
Err: Input string was not in a correct format.
5/28/2015 12:09:36 PM - ERRO - Error at workertranaction_DoWork.
Probable I/O race condition detected while copying memory. The I/O package is not thread safe by default. In multithreaded applications, a stream must be accessed in a thread-safe way, such as a thread-safe wrapper returned by TextReader's or TextWriter's Synchronized methods. This also applies to classes like StreamWriter and StreamReader.

Code for getting database and backgroundworker:-
VB
Private Sub bgworker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgworker.DoWork
    Try
        Dim workIsCompleted As Boolean = False
        While (Not bgworker.CancellationPending) AndAlso (Not workIsCompleted)
            If obj_conn.GetAllAdvertisement() Then
                Dim result As Boolean
                If Dispatcher.CheckAccess() Then
                    result = LoadAllImage(sender)
                Else
                    result = Dispatcher.Invoke(New MyFuncHandler(AddressOf LoadAllImage), sender)
                End If
            Else : App.LogEvents("File not found.", EventLogEntryType.Information)
            End If
            workIsCompleted = True
        End While
    Catch ex As Exception
        strMsg = "Error at workertranaction_DoWork." & vbCrLf & ex.Message
        App.LogEvents(strMsg, EventLogEntryType.Error)
    End Try
End Sub


VB
Public Function GetAllAdvertisement() As Boolean
    Dim cmd As MySqlCommand = Nothing
    Dim rdr As MySqlDataReader = Nothing
    Dim strSQL As String
    Dim imgContent() As Byte = Nothing
    Try
        Try
            If (Not IsNothing(cnDB)) Then
                If Not (cnDB.State = ConnectionState.Open) Then
                    cnDB.Open()
                End If
            Else
                strMsg = "Error in verifying MySQL connnection."
                App.LogEvents(strMsg, EventLogEntryType.Error)
                Return False
            End If
        Catch ex As Exception
            strMsg = "Exception in verifying MySQL connnection."
            App.LogEvents(strMsg & vbCrLf & "Err: " & ex.Message, EventLogEntryType.Error)
            Return False
        End Try
        strSQL = "SELECT * from tbl_advertisement where orientation='Vertical';"
        cmd = New MySqlCommand(strSQL, cnDB)
        cmd.CommandType = CommandType.Text
        rdr = cmd.ExecuteReader()
        If rdr.HasRows Then
            arr_ads = New ArrayList
            While rdr.Read
                If Not IsDBNull(rdr("arr_image")) Then
                    imgContent = rdr("arr_image")
                Else
                    imgContent = Nothing
                End If
                arr_ads.Add(imgContent)
            End While
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        strMsg = "Error get advertisement image."
        App.LogEvents(strMsg & vbCrLf & "Err: " & ex.Message, EventLogEntryType.Error)
        Return False
    Finally
        If (Not IsNothing(rdr)) Then
            rdr.Close()
            rdr = Nothing
        End If
        If (Not IsNothing(cmd)) Then
            cmd.Dispose()
            cmd = Nothing
            cnDB.Close()
        End If
    End Try
End Function



How to close DataReader which on another class(obj_connection) from First Page?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900