I have a web page with a list of recipes. In my code, I browse the page and load each recipe. Here is the pseudo-code of what I am doing:
public Task ScrapeAsync(){
return Task.Run(async () => {
string WebPage;
WebRequest request = WebRequest.Create(link);
request.Method = "GET";
using (WebResponse response = await request.GetResponseAsync())
using (StreamReader reader = new StreamReader(response.GetResponseStream())) {
WebPage = await reader.ReadToEndAsync();
}
}
}
I used Task.Run because the method has both asynchronous and blocking code.
public Task GetRecipeListAsync(){
return Task.Run(async () => {
}
}
In the form, he scans the list of pages and does await RecipePage.GetRecipeList()other things.
Here where the for loop is:
private async void go_Click(object sender, EventArgs e){
for (int i = (int)startingPageNUD.Value; i <= (int)finishingPageNUD.Value; ++i) {
RecipePage page = new RecipePage(page + i);
await page.GetRecipeListAsync();
}
}
My problem is that whenever an exception occurs in a method ScrapeAsync, Visual Studio 2013 points toApplication.Run(new Form1())
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
, Reflection.TargetInvocationException. . , NullReferenceException , .
- , . ?
. async/wait Task?