Vb.net 2008 : Background process halting the UI thread (or at least appears to)

R

Rob W

Greetings,

There is a small delay of around 4-5 seconds where the form freezes before
all form elements are displayed as its run the background process that
appears to halt the UI thread (code below)

Any ideas of how to process the reading and processing of the text files in
a manner which will least be noticed by the user?

Thanks Rob

Private Sub frmAddMember_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Me.BackgroundWorker1.RunWorkerAsync()

End Sub



Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

titleLookup = New String() {" ", "Dr", "Prof", "Miss", "Ms", "Mr", "Mrs"}

townLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\towns.txt", System.Text.Encoding.Default)

cityLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\cities.txt", System.Text.Encoding.Default)

countyLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\counties.txt", System.Text.Encoding.Default)

STDcodeLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\phonecodes.txt", System.Text.Encoding.Default)

End Sub



Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
BackgroundWorker1.RunWorkerCompleted

'Populate address comboboxes from THREE textfiles in the startup path dir

'No need to sort items as already sotred in text file

'Note: The address data is NOT relational (maybe future edition)

Me.cboTitle.Items.AddRange(titleLookup)

Me.cboTown.Items.AddRange(townLookup)

Me.cboCity.Items.AddRange(cityLookup)

Me.cboCounty.Items.AddRange(countyLookup)

Me.cboSTDCode.Items.AddRange(STDcodeLookup)

End Sub
 
R

Rob W

There was no noticeable improvement and for whatever reason the UI thread
appears to halt/freeze leaving a corrupt looking form onscreen and only when
the background process is complete does the form appear normally.
 
F

Family Tree Mike

Rob W said:
Greetings,

There is a small delay of around 4-5 seconds where the form freezes before
all form elements are displayed as its run the background process that
appears to halt the UI thread (code below)

Any ideas of how to process the reading and processing of the text files in
a manner which will least be noticed by the user?

Thanks Rob

Private Sub frmAddMember_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Me.BackgroundWorker1.RunWorkerAsync()

End Sub



Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

titleLookup = New String() {" ", "Dr", "Prof", "Miss", "Ms", "Mr", "Mrs"}

townLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\towns.txt", System.Text.Encoding.Default)

cityLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\cities.txt", System.Text.Encoding.Default)

countyLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\counties.txt", System.Text.Encoding.Default)

STDcodeLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\phonecodes.txt", System.Text.Encoding.Default)

End Sub



Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
BackgroundWorker1.RunWorkerCompleted

'Populate address comboboxes from THREE textfiles in the startup path dir

'No need to sort items as already sotred in text file

'Note: The address data is NOT relational (maybe future edition)

Me.cboTitle.Items.AddRange(titleLookup)

Me.cboTown.Items.AddRange(townLookup)

Me.cboCity.Items.AddRange(cityLookup)

Me.cboCounty.Items.AddRange(countyLookup)

Me.cboSTDCode.Items.AddRange(STDcodeLookup)

End Sub

Have you tried calliing the RunWorkerAsync() in the form's shown event?

Mike
 
R

Rob W

Thanks I will give that a try, I didn't even know there was such an event
(never paid notice to it before).

Cheers
 
C

Chris

Greetings,

There is a small delay of around 4-5 seconds where the form freezes before
all form elements are displayed as its run the background process that
appears to halt the UI thread (code below)

Any ideas of how to process the reading and processing of the text files in
a manner which will least be noticed by the user?

Thanks Rob

Private Sub frmAddMember_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

Me.BackgroundWorker1.RunWorkerAsync()

End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e
As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

titleLookup = New String() {" ", "Dr", "Prof", "Miss", "Ms", "Mr", "Mrs"}

townLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\towns.txt", System.Text.Encoding.Default)

cityLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\cities.txt", System.Text.Encoding.Default)

countyLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\counties.txt", System.Text.Encoding.Default)

STDcodeLookup = IO.File.ReadAllLines(My.Application.Info.DirectoryPath +
"\lookupdata\phonecodes.txt", System.Text.Encoding.Default)

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
BackgroundWorker1.RunWorkerCompleted

'Populate address comboboxes from THREE textfiles in the startup path dir

'No need to sort items as already sotred in text file

'Note: The address data is NOT relational (maybe future edition)

Me.cboTitle.Items.AddRange(titleLookup)

Me.cboTown.Items.AddRange(townLookup)

Me.cboCity.Items.AddRange(cityLookup)

Me.cboCounty.Items.AddRange(countyLookup)

Me.cboSTDCode.Items.AddRange(STDcodeLookup)

End Sub

How many items in each of the combos? Have you tried calling
BeginUpdate for each combo and then calling EndUpdate after you have
added the items? For Example:

Me.cboTitle.BeginUpdate()
Me.cboTitle.Items.AddRange(titleLookup)
Me.cboTitle.EndUpdate()

I'm not sure it's necessary with AddRange, but it might help.

Chris
 
R

Rob W

I'm just going to try your suggested method of using BeginUpdate() and
EndUpdate() after I've had a quick read of what they can be used for.

Now this is where I may get raised eyebrows or frowns but he largest text
file is towns containing the names of all UK towns on separate lines, that's
40K lines.

So I expect some physical file access delay, however at the moment if the
even happens in form load (looks odd then appears) or form show (form shows
instantly but then locks up for a few seconds) in both instances the user
will be left bewildered to what's actually happening.

I did think about a database table, not sure how much that could improve
performance.
 
Top