Updating counter during data load

  • Thread starter Thread starter Frank
  • Start date Start date
F

Frank

I am loading data through a OleDbDatareader. The load is
started from a form by the user, but the actual load is
done in a separate class, which is accessed from my form.

Now, I want the user to see how the load is progressing,
so I have a counter textbox, which I have passed to the
load object as a texbox (ByRef). For every 100 records
that are loaded, I update the Text attribute for the
textbox with a new number. However, the textbox don't
seem to show any number until the load is over; at that
point it does show the number of records loaded. I
therefore assume that the textbox does get updated, but
that perhaps and update of the form and its objects,
isn't performed until after the load.

Any hints to how I can force the textbox object to be
updated on the screen?


Regards,

Frank
 
Hi Frank,
The code beneath is from Armin I did send an hour ago to Crirus,
if you have problems with it, you can do a less nice code like this (rough
typed)
\\\
do while x ' and x is a boolean wich you have public in the class that reads
the data
threadin.thread.sleep(50)
application.doevents
loop
///

But this is much nicer, I did not try it till now, but it looks so nice.

\\\ by Armin
Private m_Thread As MyThread
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
m_Thread = New MyThread
AddHandler m_Thread.Progress, AddressOf OnProgress
AddHandler m_Thread.Done, AddressOf OnDone
m_Thread.Start()
End Sub
Public Delegate Sub ProgressDelegate(ByVal Progress As Integer)
Private Sub OnProgress(ByVal Progress As Integer)
If Me.InvokeRequired Then
Me.Invoke(New ProgressDelegate( _
AddressOf OnProgress _
), New Object() {Progress})
Else
Me.Button1.Text = Progress.ToString
End If
End Sub
Private Sub OnDone()
m_Thread = Nothing
End Sub
///
\\\
Class MyThread
Public Event Progress(ByVal Progress As Integer)
Public Event Done()
Private m_Thread As Thread
Public Sub Start()
m_Thread = New Thread(AddressOf ThreadStart)
m_Thread.Start()
End Sub
Private Sub ThreadStart()
Dim i As Integer
For i = 1 To 100
Thread.Sleep(100)
RaiseEvent Progress(i)
Next
RaiseEvent Done()
End Sub
End Class
///
 
Frank said:
I am loading data through a OleDbDatareader. The load is
started from a form by the user, but the actual load is
done in a separate class, which is accessed from my form.

Now, I want the user to see how the load is progressing,
so I have a counter textbox, which I have passed to the
load object as a texbox (ByRef). For every 100 records
that are loaded, I update the Text attribute for the
textbox with a new number. However, the textbox don't
seem to show any number until the load is over; at that
point it does show the number of records loaded. I
therefore assume that the textbox does get updated, but
that perhaps and update of the form and its objects,
isn't performed until after the load.

Any hints to how I can force the textbox object to be
updated on the screen?

There are two ways: Application.DoEvents or multithreading.

Concerning multithreading:
http://groups.google.com/[email protected]
 
Back
Top