I'm designing code for a download manager, and i was wondering if there are some good known patterns for the async operations?
I'm asking this because i have just started developing my own pattern.
Download a single file itself is an async operation with start, stop, pause, cancel, showing progress and speed. Download one big file can actually download many small files or parts, so this is one big operation that uses many sub-operations and the big operation should support start, stop, pause, cancel, showing progress and speed with fully consistency with the sub-operations.
After downloading, i should hash the file to validate it, and this is another operation.
You can see that i need a general way to handle all of those operations...
public interface IOperation
{
event EventHandler<StateEventArgs> StartRequested;
event EventHandler<StateEventArgs> Started;
event EventHandler<ProgressEventArgs> ProgressChanged;
event EventHandler<SpeedEventArgs> SpeedChanged;
event EventHandler<StateEventArgs> PauseRequested;
event EventHandler<StateEventArgs> Paused;
event EventHandler<StateEventArgs> ContinueRequested;
event EventHandler<StateEventArgs> Continued;
event EventHandler<StateEventArgs> CancelRequested;
event EventHandler<StateEventArgs> Cancelled;
event EventHandler<StateEventArgs> Completed;
event EventHandler<ExceptionEventArgs> WarningErrored;
event EventHandler<ExceptionEventArgs> FatalErrored;
Op开发者_C百科erationState OperationState { get; }
ISynchronizeInvoke Invokable { get; set; }
object State { get;set; }
void StartAsync();
void StartAsync(params object[] args);
void StartSync();
void StartSync(params object[] args);
void Pause();
void Continue();
void Cancel();
}
Model-View-Controller (MVC): http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller This is the pattern to design a GUI in proper way.
精彩评论