-
Background Worker
Hi:
I need help on this:
I'm using Backgound worker to process a continous string, break it in 4 character and store into string array. I'm able to process the string, but how do I pass the string to UI say Textboxes or labels asyncronously? Using Delegates? Can someone show me some sample?
Thansk
-
Re: Background Worker
Yes, you could use a delegate but that would mostly defeat the purpose of using a BackgroundWorker. The BGW provides the ProgressChanged and RunWorkerCompleted events to enable you to interact with the UI without delegation. You would know all this already though, from reading the MSDN documentation for the BackgroundWorker class.
Follow the BGW link in my signature for a code example. It's in VB but you should have no trouble getting the principle, which you can then implement yourself in C#. You might also want to run the code through a converter, although the result may not be 100% ideal or correct, depending on which converter you use.
-
Re: Background Worker
Thanks jmcilhinney! I have a question though, If I would to used progresschange event, I have 30 array of string when I break using substring function, then how do I pass it back to the textboxes (30 textboxes) in proper order? The backgroundWorker1.ReportProgress have a integer member and userstate, the integer member is (%) and the only meant is thru the userstate. So how could I do it?
:confused:
Thanks
-
Re: Background Worker
The Integer value passed to ReportProgess is supposed to represent the progress percentage, as you say. The UserState is type Object in order that you be able to pass absolutely any object you like. You could pass anything from a simple Byte to a DataSet containing 1000 tables each with a million rows and DataRelations between them.
You can declare your own class that has two properties: one a TextBox and one a String. Each time you call ReportProgress you create an instance with the appropriate TextBox and String. In the ProgressChanged event handler you cast the UserState as that type, then assign the String to the Text property of the TextBox.