|
-
Apr 23rd, 2013, 04:29 AM
#1
Re: Correct way to use the BackgroundWorker
The way you are doing it is completely different than what I showed you...
It seems like you want to create a new instance of your ProgressForm from inside the worker thread, and use that in the ProgressChanged event. I won't recommend this approach. UI elements like any controls or forms must be created on the UI thread only, otherwise they will usually cause cross-thread operation problem. You don't get any errors at present because you never use that form instance. It is as good as passing a boolean value.
Besides this, everytime you report the progress, a new instance of your ProgressForm will be created. And you never use that form instance! So you just keep on filling your memory space unnecessarily. It will ultimately get garbage-collected. But that will also consume your precious CPU cycles. This is not the correct approach anyways.
The other problem is, you only show the form. You never give any UI updates. Calling the frmProgress.Show() method repeatedly will have no effect. It is same as calling it once.
This may be according to your requirements, but I would have preferred updating a Label or ProgressBar on the ProgressForm. This way the user at least knows what percentage of the work is done and how much more time he should expect to wait before it is fully completed.
If you examine the approach I showed you, the following things differ which you should consider:
1. Only one instance of the ProgressForm is created for the lifecycle.
2. The ProgressForm gives information about how much percentage of the task is done. (I just showed you how to update a Label, but you can use other controls like ProgressBar etc.)
3. I don't do the "New ProgressForm" with declaration. I do it just before I start the BackgroundWorker task. This way I don't need to keep an instance of that ProgressForm in memory until I actually begin doing the time consuming task, and that form really needs to be shown.
-
Apr 23rd, 2013, 05:33 AM
#2
New Member
Re: Correct way to use the BackgroundWorker
Pradeep1210 thank you.
Details of making new instance each time reporting progress and GC were really helpful.
I have used progressbar to show the amount of progress in another scenario and in this one I want to show only the 'Please wait ...' or 'loading ...' which is sufficient enough for now.
I changed the code as below:
Code:
'at class level
Private frmProgress As ProgressForm
'at start Button method body
frmProgress .show()
BGW.RunWorkerAsync()
In BGW_DoWork :
worker.ReportProgress(100)
'Time consuming job goes here
'In BGW_ProgressChanged :
'In BGW_RunWorkerCompleted :
frmProgress.Close()
But, sorry for this silly question, you specified (Me) on show method of the form. Does it refer to the same form as buuton1 is placed in?
I took a look at msdn Form show Method (http://msdn.microsoft.com/en-us/library/szcefbbd.aspx) It says 'owner As IWin32Window' as the argument of the method.
The actual question: How can I be sure to close the 'Please Wait ...' form on BGW completed event. AndAlso what is the best argument for report progress which does not have any idea about stage of the work, just 'Please wait ...".
Thanks again very much for your time and nice detailed explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|