Quote:
Originally Posted by Solitaire
The following works for me:
|
But, it doesnt work for the OP
He wants to count to.. say.. a million.. And he wants to see the numbers rolling up in the text box from 1 to a million (or the text extending each time). He does not want to click the Go button, and have the UI hang for 30 seconds while
VB concatenates a million strings together and stores them in a text box and only reveals the result at the end, hence why MattP provided the BackgroundWorker example
hscorpio: note that MattP's example replaces the text each time. If you want to append the text each time, use textbox.AppendText("text") in place of textbox.Text = "text"
Note also that you DONT have to use e.ProgressPercentage (you can set it to 0 always, or use it to pass status codes) nor does it have to be a progress percentage at all; it can jump up and down and be totally unrelated to your operation's progress.
You can pass any information you like in the UserState parameter; MattP passes a number, i, but it can be a string, number, error, anything. At the other side, in the ProgressChanged event handler, convert the userstate back to what you want with DirectCast(e.UserState, <type>).
e.g. Suppose you had a custom class ReportState, you would
Code:
Dim myReportState as New ReportState
...
BackgroundWorker1.ReportProgress(i / 100, myReportState)
...
'and in the progress changed event handler:
...
Dim tmpRepState as ReportState = DirectCast(e.UserState, ReportState)
'now do whatever you want with the reportstate object
As an example, when using a background worker, i'll use the progress percentage as the level of logging from 0 to 5 (low to high) and the userstate as the message i want to log. You can also use the TypeOf() command to see what kind of object the UserState is, and perform different actions depending on what it is