Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone!
i implemeted C# Get Only Certian Text From Wiki Page?[^] to download all the text from a wiki search( i am using the wiki api to do this) now the problem is that i was using the DownloadString method, and when its called the UI thread freezes up until the string is downloaded(and if the net is slow it will sit there for a while). So in order to solve this i need to use the DownloadStringAsync method but i can't figure out how to use it
here is the original code i am currently using:
C#
public void WikiSearchEngine(string SearchInput)
{
    //Download The Search Page
    var webClient = new WebClient();
    string pageSourceCode = webClient.DownloadString("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + SearchInput);
}

here is what i came up with inorder to try to use the Async version of this method:
C#
public void WikiSearchEngine2(string SearchInput)
{
    //Download The Search Page
    var webClient = new WebClient();
    string StringToConvert = "http://en.wikipedia.org/w/api.php?action=opensearch&search=" + SearchInput;
    Uri StringToUri = new Uri(StringToConvert);
    string pageSourceCode = webClient.DownloadStringAsync(StringToUri);
}

now the problem is that VS gives me an error saying: Cannot implicitly convert type 'void' to 'string'
so what am i doing wrong?
i have already looked at the reference for this method and it didn't really help.
thank you for your help in advance,
MasterCodeon
Posted
Updated 13-Dec-17 9:11am

1 solution

use DownloadStringCompleted [^]event as below

C#
var client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
   string pageSourceCode = e.Result;
   //do something with results 
};

client.DownloadStringAsync(StringToUri);
 
Share this answer
 
v2
Comments
MasterCodeon 9-Jan-15 10:19am    
it works great thank you.
i so i guess that the error resulted from me trying to put the contents on what the DownloadStringAsync method's return value(which is void i think) into the string right?
thank you so much!
DamithSL 9-Jan-15 10:20am    
yes, correct
MasterCodeon 9-Jan-15 10:31am    
ah i understand now.
thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900