When performing a long operation in your开发者_开发百科 application, how do you promote information from your business tier to your UI?
Events? Passing in a status object which is hooked to the UI?
Before I start I just wondered what cunning ways you guys and girls have come up with.
There are two ways I usually do this:
- Pushing data with events. Quite simply, the long method knows how much it's done and whenever it finishes a piece it raises an event and pushes data to the UI.
- Polling. Suppose the method is for reading a file. It will always update a property somewhere with its progress, and the UI will poll that property once every 100-200ms to update the progress. The reason for 100-200ms is that lower than that the user won't notice and it will only slow down the operation; higher and the progress would be 'clunky'.
You should make a difference between: this can be done in a GUI-app and this needs to be done in batch.
Something that runs for 5 minutes: in your gui-app or in batch?
If we consider a typical Web app - one that is not using a push technology such as COMET, updated information will be fetched by the browser intermittely, perhaps when the user requests, perhaps when an AJAX background call is made.
In that scenario I have my service offering a Status operation.
int requestSomeWork( ... data ... ) ; // returns an id
Status getStatus( int id ); // tell me about a previously submitted request
This style is useful not only for displaying progress information but also in recovery from failure scenarios. I may provide additional methods so that we can determine what happened if the Browser or Server crashes just as we were creating a request
int[] findRequest( ... search Criteria ... ); // I think I submitted a request like ... what's its id?
If we have a "live" UI, either thick client or COMET then we can add event processing
void registerForUpdates( id, callMe );
This seems exactly what events are designed for. Letting other objects know about changes to that object.
Having an ProgressUpdated event that reports a progress percentage or something along lines is how I would do it.
精彩评论