Insert into database from arraylist

Hi all I have a problem I can not solve.

On many occasions I need to insert into database records that
taking place in real time and at great speed and always
encounter with the problem that information is lost. I had thought
use an arraylist to insert the data and another thread turned
every x seconds to cross it and write to the db, but I can not put
running because I have errors like "Collection was modified; can
not to execute the operation of that list.. Surely there is a
way to go.

Extensive information:

I'm working on reading a value using a socket, which changes each
100 ms and I have to store it in database each and every one of
them, and my idea was first stored in an arraylist, I checked
not lose any records. Now the problem is to pass
bd, without the process stops, ie the reading continues
I can not wait for a pause to store as the process can
take hours to stop.
Right now I have this code simplified test.
Dim pila_1 As New ArrayList
  Dim Hilo_inserta_pila As New Thread(AddressOf lee_e_inserta_pila) 


'.....

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
         pila_1.Add(Valor_leido)
    End Sub


    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If (Hilo_inserta_pila.ThreadState And ThreadState.Unstarted) <> 0 Then
            Hilo_inserta_pila.Start()
        ElseIf Hilo_inserta_pila.ThreadState = ThreadState.Stopped Then
            Dim Hilo_inserta_pila As New Thread(AddressOf lee_e_inserta_pila)
             Hilo_inserta_pila.Start()
        End If
    End Sub


    Private Sub lee_e_inserta_pila()

        Dim obj As Object
        Dim pila_temp As New ArrayList
        pila_temp = pila_1

        For Each obj In pila_temp
            Table_Adapter.Insert_in_BD(obj, Format(Now, "yyyy/MM/dd"), Format(Now, "HH:mm:ss"))
        Next
        pila_temp.Clear()

    End Sub

Open in new window



VB.NET
MySQL

Thank you very much.
YawesAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
You can use a structure such as a Queue where one process is adding values and another thread is dequeuing values and saving to DB.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.