收集被修改;操作可能无法执行 [英] Collection was modified; Operation may not execute

查看:74
本文介绍了收集被修改;操作可能无法执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序,从串口获取数据,然后将其保存在文本文件中。

,同时保存文本文件中的数据。我同时读取数据并创建折线图。但我收到这样的问题..



收藏已修改;操作可能无法执行



和这个



无法访问该文件,因为它正被另一个程序使用



on backgroundworker1



  Dim  ChartValues 作为 StreamReader =  StreamReader(strDATPath)
ChartValues.Peek< ;> -1
Dim arr() As String = ChartValues.ReadLine.Split( c)
如果 arr.Length = 7 那么
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 ))
结束 如果
结束 虽然







on backgroundworker2





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

showdetails()



' 创建文件目录
如果 Directory.Exists( C:\ SP_Log \ + strDateToday + \然后
Directory.CreateDirectory( C:\SP_Log \ + strDateToday + \
结束 如果

' 创建文件
如果 N. ot File.Exists(strDATPath)然后
使用 swLogWriter As StreamWriter = File.CreateText(strDATPath)
结束 使用
其他
使用 swLogWriter As StreamWriter = File.AppendText(strDATPath)
如果 tbIn.Text<> Nothing 然后
swLogWriter.WriteLine(tbIn.Text)

结束 如果

结束 使用
结束 如果





on timer tick



 如果 SerialPort1.IsOpen()那么 


< span class =code-comment>' createlog()
如果 BackgroundWorker2.IsBusy = False 然后
counterCurrent = 0
BackgroundWorker2.RunWorkerAsync()

结束 如果

如果 BackgroundWorker1.IsBusy = False 那么
counterCurrent = 0
BackgroundWorker1.RunWorkerAsync()

结束 如果

' tbIn.Text =


结束 如果







应该是什么问题?

解决方案

< blockquote> 文件是线程间通信的不良选择( BackgroundWorker )。

System.Collections.Concurrent.BlockingCollection(Of T)是des准确地说明了您似乎需要的线程间通信类型。



如果文件只是进行通信,那么请删除它完全是。


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()



       '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

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?

解决方案

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.


这篇关于收集被修改;操作可能无法执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆