Results 1 to 29 of 29

Thread: [RESOLVED] strange issue code dos not continue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Resolved [RESOLVED] strange issue code dos not continue

    i have this code in BackgroundWorker to fill datagridview
    vb.net Code:
    1. Dim sql As String = "select bot.trigger,bot.verse,bot.verse1 from bot"
    2.         Dim Adapter As New OleDbDataAdapter(sql, CON)
    3.         Dim table As New DataTable
    4.         Adapter.Fill(table)
    5.         CON.Close()
    6.         DataGridView2.DataSource = table

    the problem is after Dim table As New DataTable the code dos not continue to next line and exit the BackgroundWorker1 sub

    but when i put messagebox.show("") before Dim table As New DataTable the code continue and fill the datagrid

    and solution for this strange issue

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    Code:
    Dim sql As String = "select trigger, verse, verse1 from bot

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    same issue

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    i fixed it
    in vs2013 it was work in
    vb.net Code:
    1. Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    2.  
    3.     End Sub

    but i did change it to

    vb.net Code:
    1. Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    2.  
    3.     End Sub

    and now it work fine

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    If your code crashes after Adaptor.Fill(table), there is either an error in your sql string, or an error in your connection string.
    Check your field names are correct, your table name is correct, and check your connection string...

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    The problem was referencing UI controls from a secondary thread.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    my application were not crashing at all
    when i put the code in form load it fill that datagridview but when i put it in BackgroundWorker1_DoWork
    it start but without filling the datagridview

    what do you mean The problem was referencing UI controls from a secondary thread

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    The BackGroundWorker_DoWork sub does not run on the same thread as the form which runs on what is known as the UI (user interface) thread. The BackGroundWorker_RunWorkerCompleted sub runs on the UI thread.

    The problem was referencing controls cross thread, meaning the form's controls are not accessible on the BackGroundWorker thread without invoking them on that thread.

    Try googling BackGroundWorker amd cross threading

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    i see so how can i fix referencing UI controls from a secondary thread

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    Add this code to your form:

    Code:
    Private Delegate Sub setDGVDataSourceCallBack(ByVal source As Object)
    Private Sub setDGVDataSource(ByVal source As Object)
        If DataGridView2.InvokeRequired Then
            DataGridView2.Invoke(New setDGVDataSourceCallBack(AddressOf setDGVDataSource), source)
        Else
            DataGridView2.DataSource = source
        End If
    End Sub
    Then instead of:

    Code:
    DataGridView2.DataSource = table
    Use:

    Code:
    setDGVDataSource(table)

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    If you search the VB.Net CodeBank, jmcilhinney has some more in depth explanations for this topic...

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    but that wont be in another thread..the reason i made it load in BackGroundWorker is because it too big access database too many records and i wont my application to take long while starting

    so is there any way to make BackGroundWorker work normal again without add the fill to form thread

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    i tried that but same that DGV dos not fill
    vb.net Code:
    1. Private Sub setDGVDataSource(ByVal source As Object)
    2.         If DataGridView2.InvokeRequired Then
    3.             DataGridView2.Invoke(New setDGVDataSourceCallBack(AddressOf setDGVDataSource), source)
    4.         Else
    5.             DataGridView2.DataSource = source
    6.         End If
    7.     End Sub
    8.     Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    9.         Dim sql As String = "select bot.trigger,bot.verse,bot.verse1 from bot"
    10.         Dim Adapter As New OleDbDataAdapter(sql, CON)
    11.         Dim table As New DataTable
    12.         Adapter.Fill(table)
    13.         CON.Close()
    14.         setDGVDataSource(table)
    15.     End Sub

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    how about using the BackgroundWorker.ReportProgress method and attaching an appropriate handler to the BackgroundWorker.ProgressChanged event

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    vb.net Code:
    1. Private Delegate Sub setDGVDataSourceCallBack(ByVal source As Object)
    2.     Private Sub setDGVDataSource(ByVal source As Object)
    3.         If DataGridView2.InvokeRequired Then
    4.             DataGridView2.Invoke(New setDGVDataSourceCallBack(AddressOf setDGVDataSource), source)
    5.         Else
    6.             DataGridView2.DataSource = source
    7.         End If
    8.     End Sub
    9.     Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    10.         Try
    11.             Dim sql As String = "select trigger,verse,verse1 from bot"
    12.             Dim Adapter As New OleDbDataAdapter(sql, CON)
    13.             Dim table As New DataTable
    14.             Adapter.Fill(table)
    15.             CON.Close()
    16.             setDGVDataSource(table)
    17.         Catch ex As Exception
    18.             MessageBox.Show(ex.Message)
    19.         End Try
    20.     End Sub

    Error : object reference not set to an instance of an object
    it show the error for Dim table As New DataTable i changed it to table1111 and still the same error
    Last edited by new1; Nov 1st, 2015 at 07:24 AM.

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    i think the problem is in my application
    i tried the code in new test application it work fine
    but dos not work in original application
    is there away to fix it

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    You misunderstood me:

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim sql As String = "select bot.trigger,bot.verse,bot.verse1 from bot"
            Dim Adapter As New OleDbDataAdapter(sql, CON)
            Dim table As New DataTable
            Adapter.Fill(table)
            CON.Close()
            setDGVDataSource(table)
        End Sub
    
        Private Delegate Sub setDGVDataSourceCallBack(ByVal source As Object)
        Private Sub setDGVDataSource(ByVal source As Object)
            If DataGridView2.InvokeRequired Then
                DataGridView2.Invoke(New setDGVDataSourceCallBack(AddressOf setDGVDataSource), source)
            Else
                DataGridView2.DataSource = source
            End If
        End Sub
    
    End Class

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    ok when i use BackgroundWorker1.RunWorkerAsync() in button sub it work although the program freeze for about 7 sec which it dos not suppose to

    but when i put BackgroundWorker1.RunWorkerAsync() in the form load even it give me the error : object reference not set to an instance of an object

    any way to make it work from form load event without the error or freeze the application

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    Try putting it in Form_Shown

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    i tried something and it work fine i just move BackgroundWorker1.RunWorkerAsync() up to the first line in the form load event and it work fine now but the issue it it freeze the application for 6 to 7 sec is this suppose to happen? any way to fix the delay

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    There's obviously something time consuming which is affecting your UI thread.
    There are ways to improve performance, but we need to see the full code...

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: strange issue code dos not continue

    the full code for form load event? or the full code for my application

    the form load normally but when i switch to the DGV tap then the application freeze till it load

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] strange issue code dos not continue

    Is Form1 the startup Form?
    If yes you could use the constructor:

    Code:
    Public Class Form1
    
        Public Sub New()
    
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
    
            ' Put your DGV loading code here
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' put your other loading code here
        End Sub
    
    End Class

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: strange issue code dos not continue

    In the scenario I described, you wouldn't use a BackGroundWorker.
    If your DGV is in a TabControl TabPage, it loads differently to how you'd expect, but try the constructor idea first...

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: [RESOLVED] strange issue code dos not continue

    the delay cause because autosizecolum were set for all cells and it was was big data in the DGV that what cause the delay i change it to non and it load fast without freezing the application

    but what do you mean with i wouldn't use a BackGroundWorker.
    If your DGV is in a TabControl TabPage, it loads differently to how i'd expect

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] strange issue code dos not continue

    If you load the dgv in sub new, it'll be loaded by the time the form first loads.

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: [RESOLVED] strange issue code dos not continue

    so what is the right way to load it

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: [RESOLVED] strange issue code dos not continue

    Code:
    Public Class Form1
    
        Public Sub New()
    
            ' This call is required by the Windows Form Designer.
            InitializeComponent()
    
            ' Put your DGV loading code here
            Dim sql As String = "select bot.trigger,bot.verse,bot.verse1 from bot"
            Dim Adapter As New OleDbDataAdapter(sql, CON)
            Dim table As New DataTable
            Adapter.Fill(table)
            CON.Close()
            DataGridView2.DataSource = table
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' put your other loading code here
        End Sub
    
    End Class

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Posts
    534

    Re: [RESOLVED] strange issue code dos not continue

    i used the background worker so that loading the data into the DGV wont make application start take long or freeze ..dos that code do the same?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width