BackgroundWorker (Load data from DB)

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 13267 Views - Last Post: 05 August 2011 - 03:29 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

BackgroundWorker (Load data from DB)

Posted 03 August 2011 - 08:24 AM

Hi people.
I'm trying to load data from DB with a BackgroundWorker but I'm getting the follow error:

Quote

"Cross-thread operation not valid: Control 'DataGridView1' accessed from a thread other than the thread it was created on"

Well I didn't created any other thread or call it from other place, my code is just:
Private WithEvents BGW As New BackgroundWorker
    Private Sub BGW_DoWork() Handles BGW.DoWork
        LoadAll()
    End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BGW.RunWorkerAsync()
    End Sub
Private Sub LoadAll()
        Try
            Dim query As String = "SELECT * FROM Files"
            Dim Adapter As New SQLiteDataAdapter(query, connection)
            Dim DSet As New DataSet()
            Adapter.Fill(DSet, 0)
            Me.DataGridView1.DataSource = DSet.Tables(0)
            Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: BackgroundWorker (Load data from DB)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: BackgroundWorker (Load data from DB)

Posted 03 August 2011 - 08:27 AM

Well the background thread is separate from the main thread, right? That's the cross thread issue. You created your view on your main thread, spawned a background worker, and attempted to jump from your background thread to your main thread with your data.
Was This Post Helpful? 0
  • +
  • -

#3 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 03 August 2011 - 08:52 AM

Well, yes..but how can I solve it?
I've tried to do something like this:
Dim DG As New DataGridView
        With DG
            .Location = New Point(0, 108)
            .Size = New Size(767, 316)
            Me.Controls.Add(DG)
        End With
'(...)
DG.DataSource = DSet.Tables(0)
'(...)

And still gives me the same error.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: BackgroundWorker (Load data from DB)

Posted 03 August 2011 - 09:08 AM

Did you read the threading tutorials?
http://www.dreaminco...kground-worker/

You should be able to access (from the GUI) data in the background worker... so you would copy the data from the BGWorker into a new object on the main thread and set that object to the datasource of your grid.
Was This Post Helpful? 0
  • +
  • -

#5 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 06:23 AM

I tried with that example, but with no sucess:
Private Sub BGW_DoWork() Handles BGW.DoWork
        SetDG_ThreadSafe(Me.DataGridView1)
    End Sub
    Delegate Sub SetDG_Delegate(ByVal DG As DataGridView)
    Private Sub SetDG_ThreadSafe(ByVal DG As DataGridView)
        If DG.InvokeRequired Then
            Dim myDelegate As New SetDG_Delegate(AddressOf SetDG_ThreadSafe)
            Me.Invoke(myDelegate, New Object() {DG})
            LoadAll()
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 09:18 AM

What am I doing wrong? What is suppose to do?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 09:22 AM

I don't know what you are doing. Are you asking for help now with the example, or with your project? Have you read anything more on background workers than that like I provided?
Was This Post Helpful? 0
  • +
  • -

#8 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 09:53 AM

I followed the instructions in that example that you gave me, but still giving me the same error. I'm asking for help. I don't know what am I doing wrong. I just want to load data in a DataGridView with a backgroundworker.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 10:05 AM

Did you follow the section at the bottom called:

Quote

You get the following error Cross-thread operation not valid: Control 'Lbl_Status' accessed from a thread other than the thread it was created on.
Why?


I have no idea what you did with that code... or how you tried to shoe horn it into what you are doing. You need to let loose with more information.
Was This Post Helpful? 0
  • +
  • -

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 10:07 AM

Note also that you can subscribe to the RunWorkerCompleted event of the background worker. You can set the result of the operation your running on the background thread (perhaps a DataTable in your case?) in the DoWork handler method. You can then retrieve that result in the RunWorkerCompleted handler, and update the UI directly (as shown here).


Otherwise, you just need to make sure you run any long running tasks on a background thread (the UI will not hang then, and the app won't appear to 'crash'), and ensure that whenever you update the GUI from a separate thread, you invoke the update across onto the main thread using Invoke() as shown in the tutorial linked.

If .InvokeRequired is true, use Invoke() , else just update the UI normally.

Or, if you have many different 'chunks' of work to run on different threads, you could use Tasks if your using .NET 4.0.

Not sure what else I can say really :)

This post has been edited by CodingSup3rnatur@l-360: 04 August 2011 - 01:51 PM

Was This Post Helpful? 1
  • +
  • -

#11 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 10:53 AM

Hi, I did what you told to me and it works greatly, however, I am dissatisfied.
My goal with the BackgroundWorker was to not crash the application while this was making X operation. I load more than +500 lines of data to the DataGridView, and this works great and it's fast when I load data in the Load() of Form, but after load the data, I need to fill some fields in DataGridView, then I need to load, once again, more 500 "lines".
I must go through all the lines and add each line item Y. I thought that with the BackgroundWorker will not stay slow the application, however it crashes again. Any idea how to solve?
So, in the Load of form all works great with (example):
Private Sub Form1_Load() ' (...)
Dim chkbox As New DataGridViewCheckBoxColumn
        With chkbox
            .FalseValue = 0
            .TrueValue = 1
            .FillWeight = 15
        End With
        Dim view As New DataGridViewTextBoxColumn
        With view
            .HeaderText = "TypeOf"
            .Name = "TypeOf"
        End With
        Me.DataGridView1.Columns.Add(chkbox)
        Me.DataGridView1.Columns.Add(view)
        LoadAll()
End Sub

The Sub LoadAll() does what I told before (in the other post). Now I need to fill all the fields of Column "View" with some values:
    Private Sub SetDG_ThreadSafe(ByVal DG As DataGridView)
        If DG.InvokeRequired Then
            Dim myDelegate As New SetDG_Delegate(AddressOf SetDG_ThreadSafe)
            Me.Invoke(myDelegate, New Object() {DG})
        Else
            For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
                Me.DataGridView1.Rows(i).ReadOnly = True
                With Me.DataGridView1.Rows(i).Cells(1)
                    .Value = "See It"
                    .Style.ForeColor = Color.Green
                    .Style.Font = New Font("Verdana", 8, FontStyle.Bold)
                    .ReadOnly = False
                End With
            Next
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 11:09 AM

Quantify "crashes again.". Again - the more detail you can provide us the better!
Was This Post Helpful? 0
  • +
  • -

#13 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 11:15 AM

Well..if I had, imagine, 1000 lines..it would take 10/15 seconds (probably more) to be able to work with application.
Was This Post Helpful? 0
  • +
  • -

#14 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 01:06 PM

No ideas?
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2267
  • View blog
  • Posts: 9,479
  • Joined: 29-May 08

Re: BackgroundWorker (Load data from DB)

Posted 04 August 2011 - 01:17 PM

Instead of invoking Me invoke the thing you want to invoke DG !
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2