progress bar issue

jamie123

Well-known member
Joined
May 30, 2008
Messages
82
Programming Experience
Beginner
I'm using vs2008 programming in vb.net. I'm currently working on a project that reads miscellaneous text files and imports data into a sql database accordingly (Reads line, imports, loops until finished). There are 75k-150k imports to do. I tested it out the other day before using a progress bar, it takes about 5-10 minutes and works fine.

The issue I have is that while importing, if you click anywhere on the form, it will say not responding and will continue to import, but users using the program might be mistaken and think the program has locked up. I thought a progress bar would solve this, since it is responding back while the imports are going on in the background. However, the progress bar works, but as soon as you click anything on the form, it again says not responding and the progress bar stops moving (the imports will complete however). How can I isolate this import process so it can do this in the background, but will not affect the rest of the program?

Thanks!
 
its pretty easy expect for the progress bar ...

in the button (form load) where the code is started ...

if you have the code in there ... you need to put that in it own routine ... then declare a thread and start it

Dim TempThread as new threading.thread(addressof DoWorkSub)
tempthread.start

DoWorkSub has all the actual work in it ...
 
that worked great, how would i work that progress bar in there though if the perform next step is being done within the thread?
 
you have to use a delegate ...

Private Delegate Sub SetProgressBarDelgate(ByVal Value as integer)

Private Sub SetProgressBar(ByVal Value as integer)
If ProgressBar1.InvokeRequired = True Then
Dim Delegate1 As New SetProgressBarDelgate(AddressOf SetProgressBar)
Dim parameters As ListViewItem
parameters = Value
Me.Invoke(delegate1, parameters)
Else
progressbar1.value = value
End If
End Sub

so in your threaded sub ... you would call ... setprogressbar(100) ... something like that

make sense ?
 
Mostly, but need some clarification on what the Value parameter is actually meaning, what is that going to set?

Also, will I still leave my performnextstep method inside the tempthread?
 
I'm not sure what property you have to set on a progressbar to move it along ...

and what does performnextstep do ...

you probably want it to be in the thread ... but its hard to say
 
Also, it is telling me that for "parameters = Value" , integer cannot be applied to a listviewitem. How can I cast value as a listviewitem?
 
I'm going to try it in the thread. I use the performstep (sorry, not performnextstep) to move the progress bar along:
frmImportSpexxProgress.ProgressBar1.PerformStep()

That code is iterated everytime an import is done in the loop. The loop is contained in the tempthread.

So you're saying I'm going to call SetProgressBar(###) in the tempthread, before my loop is iterated?
 
if your progress bar doesn't move with the loop ... then yes ... if it does then it needs to be in your loop ...

and you can change the progressbar1.value = value to progressbar1.performstep .. if you want it to work the same ... the verison i sent you sets the progressbar to a value ...
 
Okay, everything is working good (almost) thanks very much for the help thus far.

This is my only problem.

I have the progress bar on another form than where the tempthread takes place. I have tried two different things. 1. Opening the form with the progress bar during the thread - allows the progress bar to move, but if the form is clicked it locks up that box. I also tried setting enabled = false to the form but this didn't work either. 2. Opening the forming with the progress bar before the thread starts, the form does not lock up when touched, but the progress bar does not move. What should I do?
 
Instead of getting into the nitty gritty of threading yourself, I would use the BackgroundWorker which is in the toolbox. Slap one on the form then put the code you want to run on the 2nd thread in the BW's DoWork event, The Finished event fires when it's done and if you want to ReportPgress (to update a ProgressBar or whatever) set the ReportsProgress property on the BW to true and use the ProgressChanged event to update the UI stuff to show the new progress and in the DoWork event use the BW.ReportProgress() method to fire the event and pass the data.

Much, much, much easier, especially in your case than to handle all the threading elements yourself.
 
Back
Top