Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
If anyone can resolve this for me I'll be eternally grateful! The code below is an initial attempt at a gazetteer section of a much larger application. It reads location information into a DataGridView. The code runs OK in the IDE but when compiled, it hangs when I attempt to run it. The data loads and the problem appears to be somewhere around the line - Me.CumbriaDataBindingSource.ResetBindings(False). The 'beep' indicates that the RunWorkerCompleted code is reached.

Controls are:-
two buttons (btnStart, btnStop)
a label (lblStatus)
a datagridview (datagridview1)which is bound to an Access database via the wizard.

I was convinced that I'd kept this code as simple as possible in order to get it working and the fact that I've made what I hope is a simple mistake aught to be a lesson to me. I just can't find it.

The IDE is VS2008 and the development platform is 32 bit XP

As it's quite small, the entire section of the code concerned is shown below.

VB
Public Class Form1
  Private Sub Start_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    'Clear the DataSet to allow the program to be re-run
    CumbriaDataSet.Clear()
    ' Disable the start button
    Me.btnStart.Enabled = False
    ' Enable to stop button
    Me.btnStop.Enabled = True
    ' Start the Background Worker
    BackgroundWorker1.RunWorkerAsync()
  End Sub

  Private Sub Stop_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    ' Is the Background Worker still working?
    If BackgroundWorker1.IsBusy Then
      'Cancel It
      If BackgroundWorker1.WorkerSupportsCancellation Then
        BackgroundWorker1.CancelAsync()
        ' Enable to Start Button
        Me.btnStart.Enabled = True
        ' Disable to Stop Button
        Me.btnStop.Enabled = False
      End If
    End If
  End Sub

  Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    'Fill the DataGridView from the MS Access database [Cumbria.mdb]
    Me.CumbriaDataTableAdapter.Fill(Me.CumbriaDataSet.CumbriaData)
  End Sub

  Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    'If process was cancelled by the user..
    If e.Cancelled Then
      Me.lblStatus.Text = "Cancelled"
    Else
      Beep()
      'If process was completed..
      'Force the DataGridView to refresh
      Me.CumbriaDataBindingSource.ResetBindings(False)
      'Show process completed
      Me.lblStatus.Text = "Completed"
      btnStop.Enabled = False
      btnStart.Enabled = True
    End If
  End Sub

End Class


Many thanks for any help in this. The project this is intended for is not commercial.

Rob Brookes

Modified - Unchecked Ignore HTML so code snippet would show
Posted
Updated 13-Apr-10 10:49am
v3

Very many thanks to all who offered to help with this problem. In the end it proved easier to use the background worker to load the gazetteer information (259080 records) into a dataset which has no UI. This was then queried for the location being searched for and only the results were added to the listbox. The only reason for using the background worker was that, unaided, the database takes seven seconds to load into memory during which time the primary UI is unavailable to the user and I considered a seven second splash screen a bit too much. Doing it this way no delegates are necessary and the code runs when compiled. In fact it will be part of a much larger GPS/GIS application but the gazetteer information has only recently been made available FOC from the British Ordnance Survey office so an existing search function based upon British grid references could be augmented by one searching for place names.
 
Share this answer
 
You could try and debug the program while it's running (as exe). Just select Debug -> Attach to Process in Visual Studio.
 
Share this answer
 
Many thanks for the reply Kornakar and my apologies for the delay in getting back to you. Unfortunately this didn't show any problems and everything I've so far tried has failed to resolve the issue. As such I think I'll close this post and go back to standard threading practices. The background worker just looked like an easy way to handle what should have been a simple task. Databases are not my field and it looks like it will remain that way!
 
Share this answer
 
Threads cannot modify directly controls in the GUI. you have to use delegates. The gui runs on the Main thread.

You need to use the control.invokerequiredto test this condition and then call the delegate.


The background worker also cannot return more than 1 object, so you need to use custom object to wrap the data, if you wish to return more than one object.

There are some excellent articles here on CP that you should have a read of to help you out, do a search in the articles section, here is one though to get you going;

AsyncWorker - a typesafe BackgroundWorker (and about Threading in general)[^]
 
Share this answer
 
Many thanks Dave, I will do just that. I was consfused by the fact that the code runs fine in the IDE and doesn't throw an illegal cross thread exception. I'd wrongly assumed this would be the same once the code was compiled.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900