I am creating a program that gets data from serial port then save it on a textfile.
while saving the data on the text file. I simultaneously read the data and create a line graph. but I receive a problem like this..
"Collection was modified; Operation may not execute"
and this
"cannot access the file because it is being used by another program"
on backgroundworker1
Dim ChartValues As StreamReader = New StreamReader(strDATPath)
While ChartValues.Peek <> -1
Dim arr() As String = ChartValues.ReadLine.Split(","c)
If arr.Length = 7 Then
Chart1.Series(0).Points.AddXY(arr(0), arr(1))
Chart1.Series(1).Points.AddXY(arr(0), arr(2))
Chart2.Series(0).Points.AddXY(arr(0), arr(3))
Chart2.Series(1).Points.AddXY(arr(0), arr(4))
Chart3.Series(0).Points.AddXY(arr(0), arr(5))
Chart3.Series(1).Points.AddXY(arr(0), arr(6))
End If
End While
on backgroundworker2
SerialPort1.WriteLine(tbMessage.Text)
tbIn.Text = TimeOfDay.ToString("HH:mm:ss") & "," & SerialPort1.ReadLine()
showdetails()
If Not Directory.Exists("C:\SP_Log\" + strDateToday + "\") Then
Directory.CreateDirectory("C:\SP_Log\" + strDateToday + "\")
End If
If Not File.Exists(strDATPath) Then
Using swLogWriter As StreamWriter = File.CreateText(strDATPath)
End Using
Else
Using swLogWriter As StreamWriter = File.AppendText(strDATPath)
If tbIn.Text <> Nothing Then
swLogWriter.WriteLine(tbIn.Text)
End If
End Using
End If
on timer tick
If SerialPort1.IsOpen() Then
If BackgroundWorker2.IsBusy = False Then
counterCurrent = 0
BackgroundWorker2.RunWorkerAsync()
End If
If BackgroundWorker1.IsBusy = False Then
counterCurrent = 0
BackgroundWorker1.RunWorkerAsync()
End If
End If
what should be the problem?