471,327 Members | 2,718 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

asychronicly downloading files

Hi,

I'm trying to download several files at the same time in order to
increase
performance.
I've tried to define a method that downloads a file, delegate this
method and
call BeginInvoke in a loop.
The problem is that every BeginInvoke returns an IAsyncResult and I
don't know
how to figure out when all the calls for this methods where completed.

My Code:
public class AsyncDemo
{
// The method to be executed asynchronously.
public bool TestMethod(string fileAdd, string fileName, out int
threadId)
{
threadId = AppDomain.GetCurrentThreadId();
try
{
WebClient Client = new WebClient ();
Client.DownloadFile(fileAdd, fileName);
}
catch(Exception ex)
{
return false;
}
return true;
}
}

public delegate bool AsyncDelegate(string fileAdd, string fileName, out
int threadId);

static void Main(string[] args)
{
//files to download
string[] fileUrlArr = new string[2];
string[] fileNameArr = new string[2];
fileUrlArr[0] = "http://www.site1.com/xxx.xml";
fileNameArr[0] = "file1.xml";
fileUrlArr[1] = "http://www.site2.com/xxx.xml";
fileNameArr[1] = "file2.xml";
int threadId;

// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);
IAsyncResult ar=null;

for(int i=0; i<2; i++)
{
// Initiate the asychronous call.
ar = dlgt.BeginInvoke(fileUrlArr[i], fileNameArr[i],out threadId,
null, null);
}

ar.AsyncWaitHandle.WaitOne();
}

Any help will be appreciated.
Best Regards,
Assaf

Dec 7 '05 #1
2 1019
Assaf,

After each call to BeginInvoke, place the WaitHandle returned by the
IAsyncResult implementation into an array of WaitHandles.

When you are done looping, and know how many wait handles you need to
wait on, pass the array to the static WaitAll method on the WaitHandle
class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<st*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,

I'm trying to download several files at the same time in order to
increase
performance.
I've tried to define a method that downloads a file, delegate this
method and
call BeginInvoke in a loop.
The problem is that every BeginInvoke returns an IAsyncResult and I
don't know
how to figure out when all the calls for this methods where completed.

My Code:
public class AsyncDemo
{
// The method to be executed asynchronously.
public bool TestMethod(string fileAdd, string fileName, out int
threadId)
{
threadId = AppDomain.GetCurrentThreadId();
try
{
WebClient Client = new WebClient ();
Client.DownloadFile(fileAdd, fileName);
}
catch(Exception ex)
{
return false;
}
return true;
}
}

public delegate bool AsyncDelegate(string fileAdd, string fileName, out
int threadId);

static void Main(string[] args)
{
//files to download
string[] fileUrlArr = new string[2];
string[] fileNameArr = new string[2];
fileUrlArr[0] = "http://www.site1.com/xxx.xml";
fileNameArr[0] = "file1.xml";
fileUrlArr[1] = "http://www.site2.com/xxx.xml";
fileNameArr[1] = "file2.xml";
int threadId;

// Create an instance of the test class.
AsyncDemo ad = new AsyncDemo();

// Create the delegate.
AsyncDelegate dlgt = new AsyncDelegate(ad.TestMethod);
IAsyncResult ar=null;

for(int i=0; i<2; i++)
{
// Initiate the asychronous call.
ar = dlgt.BeginInvoke(fileUrlArr[i], fileNameArr[i],out threadId,
null, null);
}

ar.AsyncWaitHandle.WaitOne();
}

Any help will be appreciated.
Best Regards,
Assaf

Dec 7 '05 #2
10x a lot Nicholas.
I'll give it a try, but it looks like the right way to do it.

Again 10x,
Assaf.

Dec 7 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Luke StClair | last post: by
reply views Thread by sales | last post: by
2 posts views Thread by Bala | last post: by
reply views Thread by just.starting | last post: by
9 posts views Thread by chad | last post: by
7 posts views Thread by Ehsan | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.