Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

VB
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


VB
SerialPort1.WriteLine(tbMessage.Text)
tbIn.Text = TimeOfDay.ToString("HH:mm:ss") & "," & SerialPort1.ReadLine()

       showdetails()



       'Create directory for the file
       If Not Directory.Exists("C:\SP_Log\" + strDateToday + "\") Then
           Directory.CreateDirectory("C:\SP_Log\" + strDateToday + "\")
       End If

       'Create the file
       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

VB
If SerialPort1.IsOpen() Then


           '  createlog()
           If BackgroundWorker2.IsBusy = False Then
               counterCurrent = 0
               BackgroundWorker2.RunWorkerAsync()

           End If

           If BackgroundWorker1.IsBusy = False Then
               counterCurrent = 0
               BackgroundWorker1.RunWorkerAsync()

           End If

           'tbIn.Text = ""


       End If



what should be the problem?
Posted
Comments
Tomas Takac 8-Oct-15 5:04am    
Where exactly is the first exception thrown ("Collection was modified; Operation may not execute")? Why exactly are you surprised to get the other exception ("cannot access the file because it is being used by another program")? Do you need to write the data into a file?
NekoNao 8-Oct-15 5:31am    
Collection was modified error was here ,, 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
NekoNao 8-Oct-15 5:31am    
and the cannot access .. is here

'Create directory for the file
If Not Directory.Exists("C:\SP_Log\" + strDateToday + "\") Then
Directory.CreateDirectory("C:\SP_Log\" + strDateToday + "\")
End If

'Create the file
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
NekoNao 8-Oct-15 5:32am    
and yes. i need to always write data on the textfile WHILE getting the data to make a graph. T_T
Tomas Takac 8-Oct-15 6:36am    
It seems like an overkill to me to write the data to a file and then read it to display it. Plus you get the locking problem of course. Why not just to display the data once you read it from the serial port and then store it to the file?

1 solution

File is a poor choice to communicate between threads (BackgroundWorker).
The System.Collections.Concurrent.BlockingCollection(Of T) is designed for exactly the type of inter-thread communication that you seem to need.

If the file is just for the communication, then drop it entirely.
 
Share this answer
 
Comments
NekoNao 3-Nov-15 22:36pm    
thanks for the answer!

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