Silverlight parallel download and serial download

Source: Internet
Author: User

After a clear line of thought, you can easily write a complete set of resource dynamic download components -Serialdownloader and Paralleldownloader, which share a complete resource table, and the serial download integrates the priority mechanism (hundred downloadpriority), parallel download also encapsulates the parallel queue pattern (queueparalleldownloader) as needed:

Downloadbase/// <summary>    ///Downloader base class/// </summary>     Public classDownloadbase {protected ReadOnly Staticlist<string> Loadeduri =Newlist<string>(); /// <summary>        ///get the downloaded completed address/// </summary>         Public Staticlist<string> Loadeduri {Get{returnLoadeduri;} }        /// <summary>        ///Download failed (error) Number of times/// </summary>         Public Static intError {Get;protected Set; } }

Paralleldownloader//<summary>///Parallel Resource downloader///</summary> public sealed class Paralleldownloader : downloadbase {//<summary>////</summary> public event triggered in resource download progress Download        Progresschangedeventhandler downloadprogresschanged; <summary>///Resources Download complete///</summary> public event Openreadcompletedeventhandler Openre        adcompleted; <summary>///Current progress percentage///</summary> public static int Progresspercentage {get; priv Ate set;        } readonly static list<string> Loadinguri = new list<string> ();        readonly static list<string> Waitinguri = new list<string> (); <summary>///Get the address currently being downloaded///</summary> public static list<string> Loadinguri        {get {return loadinguri;}} <summary>///Get waiting queue///</summary> public static list&Lt;string> Waitinguri {get {return waitinguri;}} <summary>///download resource files///</summary>//<param name= "uri" > Resource relative Address <</param&        Gt <param name= "Usertoken" > Resource parameters </param>//<param name= "Waitingtime" > if it is being downloaded, wait for detection time (in milliseconds) </ param> public void OpenReadAsync (string Uri, Object usertoken, bool iswaiting, int waitingtime) {if (Loadeduri.contains (URI))            {Download (URI, usertoken);                    } else {if (Loadinguri.contains (URI)) {//If the resource is being downloaded, wait for it to be detected every 1 seconds if it has been downloaded if (iswaiting) {DispatcherTimer timer = new DispatcherTimer () {Interval = Timespan.frommil                        Liseconds (Waitingtime)};                        EventHandler handler = null; Timer.                                Tick + = Handler = (s, e) + = {if (Loadeduri.contains (URI)) { Timer. STop (); Timer.                                Tick-= handler;                            Download (URI, usertoken);                        }                        }; Timer.                    Start ();                    }} else {if (!waitinguri.contains (URI)) {Waitinguri.add (URI)};}                    Loadinguri.add (URI);                Download (URI, usertoken); }}}///<summary>//Start download///</summary>//<param name= " URI "> Resource relative Address </param>//<param name=" Usertoken "> Resource parameter </param> void Download (String uri,            Object usertoken) {Openreadcompletedeventhandler openreadcompletedhandler = null;            Downloadprogresschangedeventhandler progresschangedhandler = null;            WebClient WebClient = new WebClient ();                Webclient.downloadprogresschanged + = Progresschangedhandler = (s, e) + = {Progresspercentage = E.progresspercentage;            if (downloadprogresschanged! = null) {downloadprogresschanged (this, e);}            };                webclient.openreadcompleted + = Openreadcompletedhandler = (s, e) + = {WebClient WC = s as webClient; Wc.                DownloadProgressChanged-= Progresschangedhandler; Wc.                openreadcompleted-= Openreadcompletedhandler;                    if (e.error! = null) {//Off-screen processing, retry after 5 seconds error++;                    Globalmethod.settimeout (Delegate {Download (URI, usertoken);                }, 5000);                    } else {waitinguri.remove (URI);                    Loadinguri.remove (URI);                    if (!loadeduri.contains (URI)) {Loadeduri.add (URI);}                if (openreadcompleted! = null) {openreadcompleted (this, e);}            }            }; Webclient.openreadasync (new Uri (URI, Urikind.relative), usertoken); }    }

 

Serialdownloader//<summary>///Download priority///</summary> public enum Downloadpriority {//        <summary>///maximum//</summary> highest = 0,//<summary>//High </summary> high = 1,///<summary>//General//</summary> Norm        Al = 2,///<summary>////</summary> low = 3,///<summary> Minimum//</summary> Lowest = 4,}///////&LT;/SUMMARY&G///serial Resource Downloader    T        public class Serialdownloader:downloadbase {//<summary>//Resources download completed///</summary>        public static event Openreadcompletedeventhandler openreadcompleted; <summary>///</summary> public static event Downloadprogresschangedeve in download progress of resources        Nthandler downloadprogresschanged; <summary&gT        Current progress percentage//</summary> public static int Progresspercentage {get; private set;}        static WebClient WebClient = null;        readonly static list<string> Loadinguri = new list<string> ();        readonly static list<string> Waitinguri = new list<string> (); <summary>///Get the address currently being downloaded///</summary> public static list<string> Loadinguri        {get {return loadinguri;}}  <summary>///Get waiting queue///</summary> public static list<string> Waitinguri {get        {return waitinguri;}} <summary>////Set up an image source for the image control///</summary>//<param name= "image" > Target picture </p aram>//<param name= "uri" > Image source Address </param>//<param name= "iswaiting" > whether to wait for the download to be completed before assigning a value </ param> public static void Setimagesource (image image, string Uri, downloadpriority priority, BOOL Iswaiting) {if (Loadeduri.contains (URI)) {image.            Source = Globalmethod.getwebimage (URI); } else {image.                Source = null;                Adduri (URI, priority); if (iswaiting) {DispatcherTimer timer = new DispatcherTimer () {Interval = Timespan.frommilliseconds (2                    000)};                    EventHandler handler = null; Timer. Tick + = Handler = (s, e) + = {if (Loadeduri.contains (URI)) {timer.                            Stop (); Timer.                            Tick-= handler; Image.                        Source = Globalmethod.getwebimage (URI);                    }                    }; Timer.                Start (); }}}///<summary>//Add preparation///</summary>//<param name= "        Uri > Image source address </param> public static void Adduri (string uri, downloadpriority priority) {    if (!waitinguri.contains (URI)) {Waitinguri.insert ((int) ((int) priority/4d) * waitinguri.count), URI);                if (Loadinguri.count = = 0) {webClient = new webClient ();                Webclient.downloadprogresschanged + = new Downloadprogresschangedeventhandler (webclient_downloadprogresschanged);                webclient.openreadcompleted + = new Openreadcompletedeventhandler (webclient_openreadcompleted);                Webclient.openreadasync (New Uri (Globalmethod.webpath (URI), urikind.relative), URI);            Loadinguri.add (URI); }} static void Webclient_openreadcompleted (object sender, Openreadcompletedeventargs e) {webcli            ENT WC = sender as WebClient; Wc.            DownloadProgressChanged-= webclient_downloadprogresschanged; Wc.            openreadcompleted-= webclient_openreadcompleted;            String uri = E.userstate.tostring (); if (e.error! = null) {//Off-screen processing, retry after 5 seconds Error++;                    Globalmethod.settimeout (Delegate {loadinguri.remove (URI);                Adduri (URI, downloadpriority.highest);            }, 5000);                } else {loadinguri.remove (URI);                Waitinguri.remove (URI);                Loadeduri.add (URI);                if (Waitinguri.count > 0) {adduri (waitinguri[0], downloadpriority.highest);}            if (openreadcompleted! = null) {openreadcompleted (sender, E);}            }} static void Webclient_downloadprogresschanged (object sender, DownloadProgressChangedEventArgs e) {            Progresspercentage = E.progresspercentage;        if (downloadprogresschanged! = null) {downloadprogresschanged (sender, E);} }    }

Silverlight parallel download and serial download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.