I have this class called SiteAsyncDownload.cs
Here's the code:
public class SiteAsyncDownloader
{
WebClient Client = new WebClient();
string SiteSource = null;
/// <summary>
/// Download asynchronously the source code of any site in string format.
/// </summary>
/// <param name="URL">Site URL to download.</param>
/// <returns>Website source code - string.</returns>
public string GetSite(string URL)
{
Uri Site = new Uri(URL);
Client.DownloadDataAsync(Site);
Client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FinishedDownloading);
//Don't know if this would work. Consult with StackOverflow.
while (true)
{
if (SiteSource != null)
{
return SiteSource;
}
}
}
void FinishedDownloading(object sender, DownloadDataCompletedEventArgs e)
{
SiteSource = Encoding.ASCII.GetString(e.Result);
throw new NotImplementedException();
}
}
I'm not 100% if this would work as I want it to. I want the class to download whatever it needs to asynchronously and when done downloading return the string. Is this the right approach?
For instance, here's an example of how I intend to use it:
SiteAsyncDownloader Downloader = new SiteAsyncDownloader();
/// <summary>
/// Search for a movie using only a name.
/// </summary>
/// <param name="MovieName">Movie name.</param>
public void SearchMovie(string MovieName)
{
string SearchURL = FormatSearch(MovieName);
string SearchSource = Downloader.GetSite(SearchURL);
string MovieURL = FindMovieURL(SearchSource);
string MovieSource = Downloader.GetSite(MovieURL);
FindInformation(MovieSource);
}
In the second line of code in the SearchMovie() method, will my program crash because it's of the async download? How can I take this into account and have it work properly?