0

I have a function as you can see here :

public class News
{

    public async Task<List<NewsContent>> parsing(string newsArchive)
    {
        List<NewsContent> lstResult=new List<NewsContent>();
        HttpClient http = new HttpClient();
        var response = await http.GetByteArrayAsync(newsArchive);
        String source = Encoding.GetEncoding("utf-8").GetString(response, 0, response.Length - 1);
        source = WebUtility.HtmlDecode(source);
        HtmlDocument resultat = new HtmlDocument();
        resultat.LoadHtml(source);

        List<HtmlNode> toftitle = resultat.DocumentNode.Descendants().Where
          (x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("news_list"))).ToList();
        var li = toftitle[0].Descendants().Where
          (x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value=="news_item")).ToList();

        foreach (var item in li)
        {
            NewsContent newsContent = new NewsContent();
            newsContent.Url = item.Descendants("a").ToList()[0].GetAttributeValue("href", null);
            newsContent.Img = item.Descendants("img").ToList()[0].GetAttributeValue("src", null);
            newsContent.Title = item.Descendants("h2").ToList()[0].InnerText;

            //finding main news content
            var response1 = await http.GetByteArrayAsync("http://www.nsfund.ir/news" + newsContent.Url);
            String source1 = Encoding.GetEncoding("utf-8").GetString(response1, 0, response1.Length - 1);
            source1 = WebUtility.HtmlDecode(source1);
            HtmlDocument resultat1 = new HtmlDocument();
            resultat1.LoadHtml(source1);
            newsContent.Content = resultat1.DocumentNode.SelectSingleNode("//div[@class='news_content_container']").InnerText;
        }
        return lstResult;
    }
}

This function just works like a robot to get the news from a website and return that as you can see above .

In my project i use a nuget called quartz to run a schedule every minute to run this function to update the news.

public class JobBackground : IJob
{

    public void Execute(IJobExecutionContext context)
    {
        News newagent = new News();
        Task<List<NewsContent>> lst = newagent.parsing("http://www.nsfund.ir");

        List<NewsContent> enresult = lst.Result;

    }
}
public class JobScheduler
{
    public static void Start()
    {
        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create<JobBackground>().Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithDailyTimeIntervalSchedule
              (s =>
                 s.WithIntervalInMinutes(1)
                .OnEveryDay()
                .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
              )
            .Build();

        scheduler.ScheduleJob(job, trigger);
    }
}

As you can see every minute this schedule calls my function to update the news.but the problem is the result never returned .

I put a breakpoint in this line in scheude

List<NewsContent> enresult = lst.Result;

but never this line executed .why ?I put the schedule in this line

Task<List<NewsContent>> lst = newagent.parsing("http://www.nsfund.ir");

And it executed .

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

0 Answers0