I want to follow the update of the site, how can I do this? What would my launched program with a certain frequency go to the site and check if there is something new there.

Here is a sample code. But the program slows down.

while(true) { // Проверка существования файла. if (File.Exists("C:\\0.txt")) { var m = File.ReadAllLines("C:\\0.txt"); foreach (var st in m) { if (st.Contains("Day")) { Shutdown(); } } } Thread.Sleep(3000); } 

How is this done more gracefully?

Closed due to the fact that the essence of the issue is incomprehensible by the participants of ixSci , aleksandr barakin , Athari , Peter Olson , Alexey Shtanko 19 Jul '15 at 12:46 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Add code to your message. So that others could feel your problem - ArchDemon
  • There is no code yet, I just need to find out where to dig - shatoidil
  • What is the actual problem? Add a timer, with an interval of an hour, the event from the timer to go to the site .. - Kromster
  • 2
    HttpWebRequest to help you. And in order not to slow down, I advise you to read about Threads threads. There is a simple component in Sharpe, but it helps you - this is BackgroundWorker - ArchDemon
  • one
    "But the program is slow." - i.e? What exactly do you dislike about the program? - pavelip

1 answer 1

There are two ways to plan actions:

  1. Through timers ( Timers or Threading ) inside the application. Such applications do as Windows-service and they work constantly.

     private static System.Timers.Timer aTimer; public static void Main() { aTimer = new System.Timers.Timer(10000); aTimer.Elapsed += OnTimedEvent; aTimer.Interval = 2000; aTimer.Enabled = true; Console.WriteLine("Press the Enter key to exit the program."); Console.ReadLine(); } private static void OnTimedEvent(object source, ElapsedEventArgs e) { Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); } 
  2. Set up a schedule for calling your program in the Windows Scheduler . The program becomes easier. Suitable for rare operations. For example, checking for updates a couple of times a day.

What do you understand as a site update? Update one specific page or all?

  1. If one, then everything is simple .

     var siteText = new WebClient().DownloadString("http://mysite.ru/news.html"); 
  2. If you want all the pages, then you need an analogue of WebCrawler , but only for the pages of one site. It works like this:

    1. visits the site at the starting addresses;
    2. loads pages from the list and searches for links to new pages;
    3. repeats point 2 for new links;

    It uses a database with a list of addresses, a job scheduling queue for downloading, a multi-threaded page load ...

    This article provides an example in C # .

Working with the contents of the pages in the html format is very convenient using the HtmlAgilityPack library. In this example, all links are selected from the html document:

 var doc = new HtmlDocument(); doc.Load("file.htm"); foreach(var node in doc.DocumentElement.SelectNodes("//a[@href"])) { var href = node["href"]; Console.WriteLine(href); }