Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

.NEt 2.0 : Threading questions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve B. - 09 Feb 2007 10:04 GMT
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
Michael Nemtsev - 09 Feb 2007 11:55 GMT
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
Giulio Petrucci - 09 Feb 2007 12:01 GMT
Hi Steve

Steve B. ha scritto:
> I'd like to know what is the difference between the class AutoResetEvent and
> ManualResetEvent.

let's suppose you're waiting for an event to be set.
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]
Brian Gideon - 09 Feb 2007 14:10 GMT
> Hi,
>
> I'd like to know what is the difference between the class AutoResetEvent and
> ManualResetEvent.

Steve,

http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml
http://www.albahari.com/threading/part2.html#_Wait_Handles

Brian
Laura T. - 09 Feb 2007 14:19 GMT
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()
}

> Hi,
>
[quoted text clipped - 43 lines]
> Thanks in advance,
> Steve

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.