4

I have a method like this:

public  List<List<string>>  GroupedNodes(string URL, params string[] XPathes)
{
    //Load HTML Source
    HtmlWeb loader = new HtmlWeb();
    HtmlAgilityPack.HtmlDocument doc = loader.Load(URL);

    //some other codes ...

    //Return result as a List of list
    return grouped;
}

I use HtmlAgilityPack to get html source from an url.

But when I use this method it causes freezing program.

I know I can use multithreading when I call this method in UI BUT I want to write my method in a way to be asynchronous & Responsive itself and when we use it, it works without freezing.

I mean if someone use my method and he/she don't know multithreading ,I want his/her program not to freeze, in the other words, I don't want to call my Method with a thread or task!

svick
  • 236,525
  • 50
  • 385
  • 514
Parsa
  • 7,995
  • 2
  • 27
  • 37

3 Answers3

1

Normally, the right way would be use async equivalents of the IO methods that you're using (like Load()). But HtmlAgilityPack does not seem to support async (at least not in its .Net 4.5 version).

This means the best option is to run your method on another thread and require callers to use async-await:

For example, the implementation could look like this:

private List<List<string>> GroupedNodes(string URL, params string[] XPathes)
{
    //Load HTML Source
    HtmlWeb loader = new HtmlWeb();
    HtmlAgilityPack.HtmlDocument doc = loader.Load(URL);

    //some other codes ...

    //Return result as a List of list
    return grouped;
}

public Task<List<List<string>>> GroupedNodesAsync(string URL, params string[] XPathes)
{
    return Task.Run(() => GroupedNodes(URL, XPathes));
}

Without using async-await, there is no good way to make your method not block the calling thread.

svick
  • 236,525
  • 50
  • 385
  • 514
0

Use AsyncCallBack

look here I Think this part of answer is what you want : Callback Model

What is AsyncCallback?

with a little change and thinking you can use it with your method

Community
  • 1
  • 1
  • This answer is too general and goes against the accepted practice in C#, which is to use `async`-`await`. – svick Sep 04 '16 at 11:29
-2

Seems you are/were not well understood concept of task and async. Asynchronous methods was born because of the nature of IO/network bound operations which can take some time to complete the operation and you had to wait until its completion. For many years events and callbacks used to solve such a problems and nowadays async and await playing the role nicely. Back to the question, when you run an async task synchronously on UI thread you must wait for its completion and that freezing is the result, otherwise if you don't want the freezing you have to use async methods either on UI dispatcher or on a ViewModel in MVVM architecture

RaminMT
  • 174
  • 1
  • 14
  • Ramin I think that your answer is too abstract to be of any use for the OP, or for anyone else that will ever read it. – Theodor Zoulias Feb 26 '21 at 20:39
  • @TheodorZoulias thanks for clarification, I felt his problem is more conceptual than technical/code so tried to explain him what's going on & why would we use async on earth and of course if he wants more description or code I'm open to ask, but I can't understand the down vote – RaminMT Feb 28 '21 at 07:41