|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
.NEt 2.0 : Threading questionsI'd like to know what is the difference between the class AutoResetEvent and ManualResetEvent. Actually, I want to use the WebClient.DownloadString method to download 8 files simultaneously in order to concatenate the content of the 8 files (no matter in which order). How can I do that ? What are the available class to reach my goal ? Is this code snippet the best way ? StringBuilder sb; public void Do(string[] urls) { WebClient wc = new WebClient(); sb = new StringBuilder(); wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); AutoResetEvent[] threadSync = new AutoResetEvent[urls.Length]; for (int i = 0; i < urls.Length; i++) { threadSync[i] = new AutoResetEvent(true); wc.DownloadStringAsync( new Uri(urls[i]), are ); } foreach (AutoResetEvent are in threadSync) { are.WaitOne(); } context.Response.ContentType = "text/css"; } private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { lock (sb) { sb.AppendLine(e.Result); } ((AutoResetEvent)e.UserState).Set(); } Thanks in advance, Steve Hello Steve B.,
Read this article http://www.albahari.com/threading/part2.html#_Wait_Handles where the difference with samples is described S> I'd like to know what is the difference between the class S> AutoResetEvent and ManualResetEvent --- WBR, Michael Nemtsev [C# MVP]. Blog: http://spaces.live.com/laflour team blog: http://devkids.blogspot.com/ "The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it" (c) Michelangelo Hi Steve
Steve B. ha scritto: > I'd like to know what is the difference between the class AutoResetEvent and let's suppose you're waiting for an event to be set.> ManualResetEvent. 1) if it's an *Auto*ResetEvent, it will automatically reset itself as you catch it's new state; AutoResetEvent a = new AutoResetEvent(false); ....some code here... a.WaitOne(); //here the execution stops until the event is not signaled //here a's state is false; 2) if it's a *Manual*ResetEvent you must manually reset its state; ManualResetEvent a = new ManualResetEvent(false); ....some code here... a.WaitOne(); //here the execution stops until the event is not signaled //here a's state is still true; a.Reset(); //now it's false HTH, Giulio - Italia -- OnAir: [nothing] On Feb 9, 4:04 am, "Steve B." <steve_bea***@com.msn.swap> wrote: Steve,> Hi, > > I'd like to know what is the difference between the class AutoResetEvent and > ManualResetEvent. > http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml http://www.albahari.com/threading/part2.html#_Wait_Handles Brian In your case, I would not use so many AutoResetEvent objects. They consume
system resources and used this way, are quite inefficient, IMO. You'd be better off with a simple counter and one event, EventWaitHandle. Like: long stillRunning; EventWaitHandle finished = new EventWaitHandle(false, EventResetMode.ManualReset); StringBuilder sb; public void Do(string[] urls) { WebClient wc = new WebClient(); sb = new StringBuilder(); // Should try to use new StringBuilder(<estimate|guess "standard" dimension>) wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); for (int i = 0; i < urls.Length; i++) { Interloced.Increment(ref stillRunning); wc.DownloadStringAsync(new Uri(urls[i]),are); } finished.WaitOne(); } private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { lock (sb) { sb.AppendLine(e.Result); } // "Last one closes the door" If(!Interlocked.Decrement(ref stillRunning) finished.Set() } Show quote "Steve B." <steve_bea***@com.msn.swap> ha scritto nel messaggio news:OpwK4HDTHHA.412@TK2MSFTNGP02.phx.gbl... > Hi, > > I'd like to know what is the difference between the class AutoResetEvent > and ManualResetEvent. > > Actually, I want to use the WebClient.DownloadString method to download 8 > files simultaneously in order to concatenate the content of the 8 files > (no matter in which order). > How can I do that ? > What are the available class to reach my goal ? > > Is this code snippet the best way ? > > StringBuilder sb; > public void Do(string[] urls) > { > WebClient wc = new WebClient(); > sb = new StringBuilder(); > wc.DownloadStringCompleted += new > DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); > AutoResetEvent[] threadSync = new AutoResetEvent[urls.Length]; > for (int i = 0; i < urls.Length; i++) > { > threadSync[i] = new AutoResetEvent(true); > wc.DownloadStringAsync( > new Uri(urls[i]), > are > ); > } > foreach (AutoResetEvent are in threadSync) > { > are.WaitOne(); > } > context.Response.ContentType = "text/css"; > } > private void wc_DownloadStringCompleted(object sender, > DownloadStringCompletedEventArgs e) > { > lock (sb) > { > sb.AppendLine(e.Result); > } > ((AutoResetEvent)e.UserState).Set(); > } > > Thanks in advance, > Steve > |
|||||||||||||||||||||||