I have written a method that downloads some files, and now I try to download it 5 files in parallel I am waiting for the rest to complete the last one, and for the rest. I have been using a ManualResetEvent for this, but it does not download anything else anymore when I include the syncronisation part (without this it works)
Here is the code from the methods:
only to read static maxFiles = 5; Static int file = 0; Fixed object files locker = new object (); Stable system threading. Manual reset event sync = new system threading. Manual reset event (true); /// & lt; Summary & gt; /// Download a file from Wikipedia asynchronously /// & lt; / Summary & gt; /// & lt; Param name = "filename" & gt; & Lt; / Param & gt; Public Zero DoanloadFileAsync (name of string file) {... System.Threading.ThreadPool.QueueUserWorkItem ((o) => {bool loop = true while (loop) if (sync.WaitOne ()) lock (filesLocker) { If (files & lt; maxFiles) {++ files, (files if == maxFiles) sync.Reset (); loop = false;}} {try WebClient downloadClient = new WebClient (); downloadClient.OpenReadCompleted + = new OpenReadCompletedEventHandler (DownloadClient_OpenReadCompleted); DownloadClient.OpenReadAsync (New Uri (url, UriKind.Absolute); // Get 5 of them here} Lock {Lock (Files Locker) {- Files; sync.Set ();} Throw;}} ); } Void downloadClient_OpenReadCompleted {Object Sender, OpenReadCompletedEventArgs E} {Try But // none of 5 get here ... Download logic ... // Works without manualResetEvent} Finally {lock (filesLocker) {- Files; Sync.Set (); }}} Am I doing something wrong?
It's written with Silverlight 4 for Windows Phone 7.
Edit:. SemaphoreSlim in a semaphore or silverlight 4
What do I mean in my comments When it was slow to use lock , when you can use the interlock apart from it it would be more demonstrative in this way.
On active 5 downloads in parallel:
Public class downloader {Private int filecount = 0; Private AutoResetEvent sync = New AutoResetEvent (wrong); Private Zero StartNewDownload {if (Interlocked.Increment (ref.fileCount)> 5) this.sync.WaitOne (); Webclient downloadclient = new webclient (); Download Client Open read + done + = downloadclient_onered; DownloadClient.OpenReadAsync (New Uri (o.toastring), Uricand.Azolute); } Private Zero downloadClient_OpenReadCompleted (Object Sender, OpenReadCompletedEventArgs E) {try download // log] goes here {} {{This.sync.Set ()); Interlock Radiation (ref. A.filecount); }} Public run zero () {string o = "url1"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); Thread.Sleep (100); O = "url2"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); O = "url3"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); Thread.Sleep (200); O = "url4"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); O = "url5"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); O = "url6"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); O = "url 7"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); Thread.Sleep (200); O = "url 8"; System.Threading.ThreadPool.QueueUserWorkItem (This. StartNewDownload, O); Thread.Sleep (400); }}
Comments
Post a Comment