.NEt 2.0 : Threading questions

S

Steve B.

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 = new AutoResetEvent(true);
wc.DownloadStringAsync(
new Uri(urls),
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
 
M

Michael Nemtsev

G

Giulio Petrucci

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
 
L

Laura T.

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),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()
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top