Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:34:18+00:00 2026-06-11T10:34:18+00:00

I am getting this error.. I was trying to do progress bar that process

  • 0

I am getting this error..

I was trying to do progress bar that process database insert method with more than 1000 record. I need the progress bar to load exactly the same process in threading..

I got the error in; cross-threading operation not valid; control ‘listview1’ accessed from a thread other than the thread it was created on.

    Imports System.IO
Public Class Form1
    Private WithEvents DT As New DataTable
    Private Delegate Sub AsyncDelegate(ByVal value As Integer)
    Private ProgressUpdater As New AsyncDelegate(AddressOf UpdateProgress)
    Private DataLoader As AsyncDelegate
    Private DataLoaderIAsync As IAsyncResult
    Private UpdateIncrement As Integer

    Private Sub LoadListAttendance()
        Dim txtLine As String = ""
        ListView1.Items.Clear()
        If File.Exists(strFileNameAndPath) = True Then
            Dim objReader As New StreamReader(strFileNameAndPath)

            Do While objReader.Peek() <> -1
                Dim strLine As String = objReader.ReadLine()
                If Split(strLine, " ")(0) = "" Then MessageBox.Show("Invalid file.", SysMsg, MessageBoxButtons.OK) : Exit Sub
                ListView1.Items.Add(Split(strLine, " ")(0))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 1, 8))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 9, 4))
                ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Mid(Split(strLine, " ")(1), 13, 2))
            Loop
        End If
    End Sub


    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        'Initialize the progress bar.

        'Run a scalar command to select the row count and set the progress maximum

        'Calculate a percentage for the Row Changed event.
        '  ie if total recs is 1,000,000 set the progress amount to 10% of that.
        '  *See DT_RowChanged

        UpdateIncrement = 100000

        'DeEnable any controls you may not want the user to touch until the 
        'load is complete.

        'Load the data Ascync
        DataLoader = New AsyncDelegate(AddressOf LoadData)
        DataLoaderIAsync = DataLoader.BeginInvoke(0, New AsyncCallback(AddressOf LoadData_Completed), Nothing)


    End Sub





    Private Sub LoadData()

        'Your Load data code.
        'This sample code loads 1,000,000 recs

        'Do not access any controls in the procedure
        If Not checkServer() = True Then frmServerSet.ShowDialog()
        For x As Integer = 0 To ListView1.Items.Count - 1
            'If RecExist_2("tbltxt", "date", ListView1.Items(x).SubItems(1).Text, "id", ListView1.Items(x).Text, False) = False Then
            sqlSTR = "insert into tbltxt (id,date,time,code) values"
            sqlSTR = sqlSTR & " ( '" & ListView1.Items(x).Text & "','" & ListView1.Items(x).SubItems(1).Text & "','" & ListView1.Items(x).SubItems(2).Text & "', '" & ListView1.Items(x).SubItems(3).Text & "')" ','" & t1 & "')"

            ExecuteSQLQuery(sqlSTR)

            'End If
        Next
        MsgBox("ok")
    End Sub

    Private Sub LoadData_Completed()

        'Async op has completed.
        'Call the endinvoke to the delegate and unlock the UI

        'Do not access any controls in this procedure

        DataLoader.EndInvoke(DataLoaderIAsync)

        Me.Invoke(New AsyncDelegate(AddressOf UnlockUI), New Object() {Nothing})

    End Sub

    Private Sub UnlockUI()
        'Do what you want to the UI Here.
        ProgressBar1.Visible = False

    End Sub

    Private Sub UpdateProgress(ByVal value As Integer)
        'Updates the progress bar from the thread it was
        'created on.
        If ProgressBar1.InvokeRequired Then
            ProgressBar1.Invoke(ProgressUpdater, New Object() {value})
        Else
            If value > ProgressBar1.Maximum Then value = ProgressBar1.Maximum
            ProgressBar1.Value = value
        End If

    End Sub

    Private Sub DT_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles DT.RowChanged
        'Handle a row change
        'Do not access any controls from this procedure

        Static counter As Integer = 1

        'first make sure the row is being added
        If e.Action = DataRowAction.Add Then
            'next check for an increment.  In this case I have a hard coded
            '100,000 Which is 10% of the million records that I know I have.

            'Do not update the progress bar on every record

            If counter Mod UpdateIncrement = 0 Then
                UpdateProgress(counter)
            End If
            counter += 1
        End If

    End Sub
End Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:34:18+00:00Added an answer on June 11, 2026 at 10:34 am

    LoadData runs on a background thread; you cannot talk to the list-view (even to read values) from this thread. More commonly. You would on the UI thread build some model from the UI (I would have a list of some custom class that expresses each of the values as properties), then make that model available to the worker thread. Then in the background thread, you only access your model that is unrelated to the UI.

    Also: don’t concatenate values to make SQL. That is suicide – it is a clear SQL injection risk. Use a parameterless query instead, or a tool that handles parameterization for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting this error while i am trying to upload a database that
So i keep getting this error when trying to connect to my database remotely.
I'm getting this error after trying to appendChild to an element that was just
I am getting this error when trying to insert data into a data table
I am getting this error when trying to iterate over a map that is
I keep getting this error when trying to delete a record from my table.
I am new to php and I am getting this error trying to load
I´m getting this error while trying to commit to a svn repository: svn: MKACTIVITY
I'm getting this error when trying to access my webservice running inside tomcat. Caused
I am getting this error when trying to merge TFS2008. There are no pending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.