Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm putting together a class library and one of the functions within it is to update a local database with information obtained from a web service.

 

I'd like to have the option to show the progress of this so have also put a small form in the library with a progress bar on it.

 

Everything works but I'd like to make the progress bar more reliable and smoother. It's the same old problem of tying up the UI thread with an intensive operation so the UI updates aren't done.

 

With a windows form application I'd have gone straight for a background worker. However with a class library this if different and I'm not sure how to approach this/whether it will work.

 

Windows Form:

 

Form Loads

Async Process started on worker

worker reports progress to form event

form updates UI

worker continues

etc.

Worker reports complete in form event

Form updates UI

 

The key point here is the form is loaded and listening for events throughout this.

 

Class library:

 

Create new object

Call update sub on object

update sub creates instance of progress bar form and starts async worker

worker reports progress to object which updates progress form

But:

 

Update sub has completed by this point so the rest of the procedures in the code that instantiated the object will have ran by this point and the object may have been set to nothing. Therefore there is nothing to report the progress to.

 

Does that make sense? I've not tested this and there could be some stuff going on int the .Net underbelly that ensure the object is kept alive until the worker has done but this doesn't feel right.

 

What I effectively want to do is tell the code to stop what it is doing but keep updating the UI as appropriate until the worker has finished.

 

Hope that explains the scenario.

 

I guess to add a little more, the code will be initially called from within a COM interop component loaded in Excel.

 

So:

 

Excel Loads

Excel calls load events on addin

Addin references library

Load events instaniate object in library and call update

 

Any suggestions really apppreciated.

 

Mark

 

p.s. The workaround to this is to use 1 thread and form.refresh with every update - I suspect this slows things down quite a bit though.

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

Still intrigued about whether anyone has any thoughts on this but I've rethought how things will work and come up with a different approach that I'm happier with anyway so thought I'd share.

 

I've now set the library up so that the object can either be run asynchronously using the background work or synchronously without it - just set boolean in the call. If it is set to synchronously then progress is not updated and the library just gets on with the work and returns. If set async then it raises progress and completion events that a form using the library can make use of to update the progress. This feels quite a neat solution that should meet all the uses I have for the library.

 

Effectively I've moved to the windows forms model I outlined earlier and am requiring the user of the library to implement a progress bar rather than having one built into the library.

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

  • Leaders
Posted

There should be no reason that you can't do this the same way in a class library that you do in a WinForms app.

 

The class library should create the form and a background worker. The logic should be executed in the background worker and the events can be handled to update the progress form. The component that kicks it all off should probably be invoked on the UI thread. The only difference will be that the code will be spread across multiple classes, but the approach is still exactly the same.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...