473,544 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(stri ng fileAdd, string fileName, out int
threadId)
{
threadId = AppDomain.GetCu rrentThreadId() ;
try
{
WebClient Client = new WebClient ();
Client.Download File(fileAdd, fileName);
}
catch(Exception ex)
{
return false;
}
return true;
}
}

public delegate bool AsyncDelegate(s tring 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(a d.TestMethod);
IAsyncResult ar=null;

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

ar.AsyncWaitHan dle.WaitOne();
}

Any help will be appreciated.
Best Regards,
Assaf

Dec 7 '05 #1
2 1052
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.co m

<st*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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(stri ng fileAdd, string fileName, out int
threadId)
{
threadId = AppDomain.GetCu rrentThreadId() ;
try
{
WebClient Client = new WebClient ();
Client.Download File(fileAdd, fileName);
}
catch(Exception ex)
{
return false;
}
return true;
}
}

public delegate bool AsyncDelegate(s tring 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(a d.TestMethod);
IAsyncResult ar=null;

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

ar.AsyncWaitHan dle.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2150
by: Luke StClair | last post by:
Only marginally belonging in this newsgroup... but oh well. I've just started writing in python, and I want to make the files available on the web. So I did the standard <a href="mypath/myfile.py"> and not surprisingly, it displays like a webpage, but just the code. If I gzip it, and then link to the new file, it will download, but its so...
0
1364
by: sales | last post by:
If you are having difficulty downloading large files, please check out www.Downloads4Dialups.com. Mention this newsgroup in the "Special Instructions" window on the Shipping Form, and receive a 20% discount. Help with downloading large files
6
3010
by: Tony K | last post by:
I have the most peculiar problem with an ASP.NET page which we use for downloading a file. When the user clicks on a link, the link points to an ASPX page which downloads the file selected. The code works fine except after the user saves the file, the links on the left side of the frameset do not work. IE locks up.
2
2419
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am impersonating the domain user account and downloading the file, It seems the impersonate is got success, but its not download the file. Its simply...
0
1622
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops downloading with the error message: "the source file could not be read". And the error is not dependent on the size of the file, as I can download a file of...
1
1694
by: Andrew86 | last post by:
I have an application which requires a number of DLLs (unmanaged ansi C dlls) to reside in the same directory as the application. I currently have these dlls included in the project folder and marked as "content". When I publish the application with ClickOnce and update my application - these dlls (along with a .chm help file that doesnt...
9
1620
by: chad | last post by:
let's say I have a folder or folders with a lot of sequential image files. I want the .net prog to package it as, maybe, a cabinet file so that downloading or uploading will be easier. Ideally, zipping it would be good, but I would like my prog to do the job instead. So, I was thinking about cabinet files, but I can't figure out how that...
0
2491
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
7
2532
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any problem: def download(url,fileName): """Copy the contents of a file from a given URL to a local file. """ import urllib
0
7438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7374
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5309
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3422
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
996
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.