PopUp Progress Bar

G

Guest

I've built a separate windows 2.0 form that i'm calling that has a progress
bar that runs text file parsing code. However when that form is called, it
doesn't display until the progress bar has reached it's maximum value or the
code has finished running. Is there any way to force the form to display
FIRST, then have the text file parsing code run while the progress bar
displays the progress?

Thanks and any help will be appreciated.
 
M

Marc Gravell

Tricky without seeing your code... however it sounds like you are running
your text parsing in a tight loop on the UI thread. The better option here
would be to go off to a secondary thread for the processing, and let the UI
stick to what it does well : painting a form.

If you already have a form and a progress bar, then the BackgroundWorker may
be your best bet. Pop this onto the form; to set it going call
RunWorkerAsync; your "real" code goes into DoWork; if you want to update the
UI call ReportProgress (from DoWork) and handle ProgressChanged. This neatly
hides away all the threading issues from your sight, so you don't need to
worry about BeginInvoke etc.

Hope this helps,

Marc
 
G

Guest

Ok. You've lost me there so here a sampling of my code:

Original form:
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Using kincylst As New KincyLST
My.Forms.KincyLST.ShowDialog()
Me.Show()
End Using
End Sub

Popup Form code:
Private Sub KincyLST_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Disable.CloseButton.Disable(Me)
GenerateFiles()
End Sub

Public Sub GenerateFiles()
Dim - declare variables here...

Me.ProgressBar1.Visible = True
Me.ProgressBar1.Minimum = 1
Me.ProgressBar1.Maximum = 100000
Me.ProgressBar1.Value = 1
Me.ProgressBar1.Step = 1

If My.Computer.FileSystem.FileExists(My.Settings.SaveLocation1 & "\"
& My.Settings.TextFileName.ToString) = True Then
Using MyReader As New
Microsoft.VisualBasic.FileIO.TextFieldParser(My.Settings.SaveLocation1 & "\"
& My.Settings.TextFileName.ToString)
MyReader.TextFieldType = FieldType.FixedWidth
MyReader.FieldWidths = StdFormat

While MyReader.EndOfData = False
Try
x = 1 : y = 1
For i As Integer = Me.ProgressBar1.Minimum To
Me.ProgressBar1.Maximum
Do While MyReader.EndOfData = False
'Extract out the individual rows from the
fixed width data text file!
currentrow = MyReader.ReadFields()

'DO STUFF....

Next currentField
Loop
Me.ProgressBar1.PerformStep()
Next
If MsgBox("The .LST File(s) Were Generated
Successfully!", MsgBoxStyle.Exclamation) = MsgBoxResult.Ok Then
Me.Close()
End If
Catch ex As Exception
catch errors!!
Finally
MyReader.Dispose()
End Try
End While
End Using
End If

End Sub
 
M

Marc Gravell

Darnit; VB ;-(

OK, rather than embarrass myself with VB.Net, I'm going to reply in C#; it
should still be fairly intelligable. I've only used anonymous delegates here
because I didn't want to have to write an entire form, as it makes for long
code; normally these would be standard event-handlers, and the "bar" and
"worker" variables would be instance variables, just like the designer would
provide.

Anyways, the point is that everything to do with reading files goes into the
DoWork handler, and everything to do with maintaining the UI goes into the
ProgressChanged handler.

Hope it translates well ;-p

Marc

Anyways, the code:

static void Main() {
using (Form f = new Form())
using (ProgressBar bar = new ProgressBar())
using (BackgroundWorker worker = new BackgroundWorker())
{
bar.Dock = DockStyle.Top;
worker.WorkerReportsProgress = true;
worker.DoWork += delegate {
int percent = 0;
while (percent<100) { // would actually exit based on
reader.read
Thread.Sleep(150); // look busy ;-p
percent++;
worker.ReportProgress(percent);
}
};
worker.ProgressChanged += delegate(object sender,
ProgressChangedEventArgs args) {
bar.Value = args.ProgressPercentage; // update the UI
};
worker.RunWorkerCompleted += delegate {
f.Close(); // exit the modal dialog
};
bar.Minimum = 0;
bar.Maximum = 100;
f.Load += delegate {
worker.RunWorkerAsync();
};
f.Controls.Add(bar);
f.ShowDialog();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top