MTA Thread Attribute Affects on Thread

1

Suppose I have the class

static class Program{
[MTAThread]
static void Main(string[] args){
Application.Run(new Form());
}
}

I simply want to apply a Thread.Sleep(1000); this keeps Form stable or locks the window equal to [STAThread] in Thread.Sleep(1000); .

NOTE: Not using BackgroundWorker to apply anything.

Ex: I have Visual C # 2008 and want to make sure I have a subscriber control of my channel on youtube. I need a WebBrowser to go to http://youtube.com/user/[MEU_CANAL]/about address and get the sourceCode of the page and run a search for the element that gives me this subscribers information and views. And until today I have not found any tool that can do this without having access to the page about of the selected channel.

    
asked by anonymous 23.04.2015 / 01:19

1 answer

1

I will leave an answer that meets your need, but using another approach. If you need to read HTML content from a Web page, you will have a hard time doing this using the WebBroser control, especially outside of a Windows Forms application. I've been able to accomplish what you need by using the System.Net.WebClient class and the HTML Agility Pack library . Just change the value of the nomeDoCanal variable to the channel you want to read the data to:

HtmlDocument doc = new HtmlDocument();
WebClient client = new WebClient();
string nomeDoCanal = "FOO";
string source = client.DownloadString(String.Format("https://www.youtube.com/user/{0}/about", nomeDoCanal));

doc.LoadHtml(source);

HtmlNode node = doc.DocumentNode.Descendants("ul").FirstOrDefault(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("about-stats"));
HtmlNode[] itens = node.Descendants("li").ToArray();
HtmlNode liSubscribers = itens[0];
HtmlNode liViews = itens[1];

string subscribers = liSubscribers.Element("b").InnerText;
string views = liViews.Element("b").InnerText;

Console.WriteLine("Subscribers: {0}", subscribers);
Console.WriteLine("Views: {0}", views);
Console.ReadKey();

To get the HTML Agility Pack library using nuget:

PM> install-package htmlagilitypack

If you can not use Nuget, you can download a version here (link Release Binaries):

link

  

Switch 1.5.5 to the desired version, on the nuget page link , on the right side, there is a link called "manual download"

The relevant HTML excerpt from the YouTube page is as follows. Keep in mind that any Youtube layout change will make this program stop working:

<ul class="about-stats">
    <li class="about-stat ">
  <b>312,992</b> subscribers
</li>
    <li class="about-stat ">
  <b>71,629,148</b> views
</li>

Library site link:

link

    
28.04.2015 / 19:56