|
-
Apr 23rd, 2013, 04:29 AM
#14
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.
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
|