Threading...

B

Bruce W. Darby

I've been doing some reading on threading, using the BackgroundWorker, but
I'm getting in over my head a bit. I've run through the tutorial at MSDN
and, while it has some good information, it is a bit buggy when it starts it
calculations. I've read a couple of books that have some information on the
BackgroundWorker, but I'm not able to wrap my mind around the concept. Is
there a 'Threading for Complete and Utter Wannabe's' out there somewhere? :)

Thanks,
Bruce
 
S

Stephany Young

Do you want to know about 'threading' or do you want to know about the
'BackgroundWorker' class?

They are two different animals.

If you realy want to know about 'threading' then that is a Ph.D. course.

If you are having trouble with the BackgroundWorker class then maybe you
would like to post the specifics of what you are having trouble with along
with your pertinent code fragments.
 
C

Cor Ligthert [MVP]

Bruce,

There are some links, however seldom is told when to use threading and when
not.

It is right to use it there where are more parallel processes busy which
have to wait on each other to can go on. Think about downloading from more
slow websites.

It is stupid to use if it is about serial processes for which everytime the
retrieving of the result cost very few time, while you need them one after
the other.

It is stupid to use if it is about getting data over a serial device, by
instance with a disk drive it can only give you more rumble.

Most that we see it used in this newsgroup is to keep the close button X
alive. I disagree with those who use for that such a processor consuming
solution as threading. However I have the idea that this is mostly only done
once to try.

Conclussion, there are seldom situations where you can use threading,
however if it can be used it is a very easy to implement solution,

Cor
 
B

Bruce W. Darby

Stephany Young said:
If you realy want to know about 'threading' then that is a Ph.D. course.

I think I'll leave the doctorate for later on down the road. :)
If you are having trouble with the BackgroundWorker class then maybe you
would like to post the specifics of what you are having trouble with along
with your pertinent code fragments.

I believe that I understand everything up to point where I actually call the
BackgroundWorker class and it creates a background thread. From that point,
it appears that all of the work is done inside the methods(?) of the class,
through DoWork, ProgressChanged and RunWorkerCompleted. These methods(?)
then pass data back to the main thread so the UI can be updated. So far, so
good, but when I run the example program, the progress bar flickers rapidly
and never gets past three or four bars...

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender as Object, ByVal
e as System.ComponentModel.ProgressChangedEventArgs) Handles
BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub

The above sub is passing back the progress information. I have THAT much
under control. :)

'Report progress as a percentage of the total task
Dim percentComplete As Integer = CInt(CSng(n) / CSng(numberToCompute) * 100)
Dim highestPercentageReached As Integer
If percentComplete > highestPercentageReached Then
highestPercentageReached = percentComplete
worker.ReportProgress(percentComplete)
End If

This portion of a function supposedly takes the data being passed back by
the worker thread and updates the progress bar. But again, the progress bar
doesn't show any progress, it flickers and goes nowhere.

Bruce
 
B

Bruce W. Darby

Cor,

You are correct in that I'm probably not using threading as a necessity, but
I am trying to understand it. I'm kind of confused by all of this and am
just seeking some light to shed on the darkness. :) Thanks for helping me to
understand a bit more.

Bruce
 
S

Stephany Young

If you are using the example from the BackgroundWorker class documentation
(the Fibonacci calculator) then did you copy and paste it verbatiom?

It works perfectly for me.

If you didn't copy and paste it verbatiom then I suggest you do so and make
sure it works before you start to 'pull it apart'.

Be aware that calculating the Fibonacci for 20 takes longer than calculating
the Fibonacci for 10, but instead of taking twice as long, the time
difference is logarithmic.

E.g. If I do it for 91 then it eaems to race up to about 40, then takes
absolutely forever to do the rest.
 
B

Bruce W. Darby

Stephany Young said:
If you are using the example from the BackgroundWorker class documentation
(the Fibonacci calculator) then did you copy and paste it verbatiom?

No, I didn't copy it at all. I tend to be visually based and learn better by
typing it in myself, so that's what I did. :)
If you didn't copy and paste it verbatiom then I suggest you do so and
make sure it works before you start to 'pull it apart'.

Not really trying to pull it apart... just trying to understand what it's
doing. Unless that's pulling it apart. :) Anyway, I'll follow your lead and
see where that leads me. You've never been wrong yet. Least, where I've been
concerned.
Be aware that calculating the Fibonacci for 20 takes longer than
calculating the Fibonacci for 10, but instead of taking twice as long, the
time difference is logarithmic.

I noticed the time factor. It actually did all that it should have done, but
the progress bar was just messed up.

Thanks Stephany
 
B

Bruce W. Darby

Stephany,

Found my little problem! :) There was a Dim statement out of place and that
appears to have the the culprit. Thanks again. Happy New Year.

Bruce
 

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