Insert into database from arraylist

Posted on 2011-05-07
Last Modified: 2012-06-22
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
    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
        ElseIf Hilo_inserta_pila.ThreadState = ThreadState.Stopped Then
            Dim Hilo_inserta_pila As New Thread(AddressOf lee_e_inserta_pila)
        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"))

    End Sub

Open in new window


Thank you very much.
Question by:Yawes
    1 Comment
    LVL 83

    Accepted Solution

    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.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Creating and Managing Databases with phpMyAdmin in cPanel.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now