同时运行两个功能时进行测试

时间:2014-06-03 05:11:27

标签: vb.net

我通过使用后台工作程序运行这两个简单的函数,(写入测试文件)和(读取文本文件)它不应该给出任何错误但是我得到了这些异常

  

mscorlib.dll中出现'system.io.ioexception'类型的第一次机会异常

任何想法如何防止这种情况?

Option Explicit On
Option Strict On

Public Class Form1

    Dim iString As String
    Dim Worker1 As Boolean = False
    Dim Worker2 As Boolean = False

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            If Worker1 = False Then
                Worker1 = True
                BackgroundWorker1.RunWorkerAsync()
            End If

            If Worker2 = False Then
                Worker2 = True
                BackgroundWorker2.RunWorkerAsync()
            End If
        Catch ex As Exception
            Debug.Print("Timer1_Tick error:" & Date.Now & ex.Message)
        End Try
    End Sub


    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Call WriteDataToFile(Date.Now.ToString("h:mm:ss tt - FFF"), "test.txt")
    End Sub

    Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        iString = GetTextDataContents("test.txt")
    End Sub

    Private Sub BackgroundWorker1_Complete(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Worker1 = False
    End Sub

    Private Sub BackgroundWorker2_Complete(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
        Worker2 = False
        Label1.Text = iString
    End Sub


    Public Sub WriteDataToFile(ByVal strData As String, ByVal strFile As String, Optional ByVal FullPath As Boolean = False)
        If FullPath = False Then strFile = System.AppDomain.CurrentDomain.BaseDirectory & strFile

        Dim notInUse = True
        While notInUse
            Try
                Using objWriter As New System.IO.StreamWriter(strFile, False)
                    objWriter.Write(strData)
                    objWriter.Close()
                    notInUse = False
                End Using
            Catch ex As Exception
                Debug.Print("WriteDataToFile error:" & Date.Now & ex.Message)
                System.Threading.Thread.Sleep(1)
            End Try
        End While
    End Sub


    Function GetTextDataContents(ByVal strFile As String, Optional ByVal FullPath As Boolean = False) As String
        GetTextDataContents = Nothing
        If FullPath = False Then strFile = System.AppDomain.CurrentDomain.BaseDirectory & strFile

        Dim notInUse = True
        While notInUse
            Try
                Dim strContents As String
                Dim objReader As System.IO.StreamReader

                objReader = New System.IO.StreamReader(strFile, True)
                strContents = objReader.ReadToEnd()
                objReader.Close()

                notInUse = False
                Return strContents
            Catch ex As Exception
                Debug.Print(Date.Now & " GetTextDataContents error " & ex.Message)
                System.Threading.Thread.Sleep(1)
            End Try
        End While
    End Function


End Class

1 个答案:

答案 0 :(得分:1)

在您写入文件时,可能会锁定该文件以进行编辑。

有关详细信息,请参阅文章 - http://msdn.microsoft.com/en-us/library/vstudio/kztecsys%28v=vs.100%29.aspx