Specific exceptions are not displayed when using async / wait and Task.Run

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:

//This is the Recipe class
//The constructor accepts a link to the recipe
//This method scrapes the recipe
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();
        }
        //Non - async code here which is used to scrape the webpage
    }
}

I used Task.Run because the method has both asynchronous and blocking code.

//This is the RecipePage class
//This method scrapes the page
public Task GetRecipeListAsync(){
    return Task.Run(async () => {
        //I get the page using WebRequest and WebResponse in the same way as above

        //Non - async/blocking code here which scrapes the page and finds links to recipes. I do await Recipe.ScrapeAsync() somewhere here.
        //And I add the recipe objects to a public list in this class
    }
}

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();
        //Some other code here
    }
}

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?

+4
3

. .

:

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();
        //Some other code here
    }
}

, , - , await. , page.GetRecipeListAsync(); , . . , , .

async void , try... catch. , , , .

, , Task async, await. async void await, .

async. , Task, . .

+1

async/wait Task ?

. Task.Run ( , HTML- , - ).

, , , async Task , , - . , ScrapeAsync .

, TargetInvocationException InnerException . async void , void: , . , ( ).

+2

getRecipeList try/catch.

catch, (-) Recipe, / , .

private string TheException = "" ;

public Task GetRecipeListAsync()
{
  TheException = "" ;
  Task result = Task.Run(async () => 
  {
    try 
    {  
      //I get the page using WebRequest and WebResponse in the same way as above
      ...
     }
     catch (Exception Ex)  
  }
  if (TheException!="") MessageBox.show(TheException) ; 
  // or Throw an exception with
  return result ; 
}

You can also test "TheExceptio" in the GoClick procedure.

+1
source

All Articles