MTA Thread Attribute affects in Thread

Asked

Viewed 114 times

1

Suppose I have the class

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

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

OBS: Not using the BackgroundWorker to apply anything.

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

  • 1

    This question is very confusing... what does it have to do MTAThread with making web requests?

  • It’s like when I make a web request the application hangs and after a long time returns. But that’s what enters Mtathread. If Stathread causes the app to crash when it calls Thread.Sleep it will be that Mtathread disrupts Thread.Sleep also?

  • I think you should reformulate the question, because currently it is difficult to understand where you want to get. Avoid using MTAThread, there is no reason to use it in this case, see this question for more details. Another thing, is needed the WebBrowser? it is not easier to make a request and process the information received?

  • MTAThread and STAThread have no connection with the Thread.Sleep, they apply only to the way certain COM objects behave in a Multi-threaded application and probably have nothing to do with your problem.

1 answer

1

I will leave an answer that meets your need, but using another approach. If your need is to read HTML content from a page on the web, you will find it very difficult to do this using Webbroser control, especially outside of a Windows Forms application. I managed to accomplish what you need using the class System.Net.WebClient and the library HTML Agility Pack. Just change the variable value nomeDoCanal to the channel you want to read the data:

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 cannot use Nuget, you can download a version here (link Release Binaries):

https://www.nuget.org/api/v2/package/HtmlAgilityPack/1.5.5

Exchange 1.5.5 for the desired version on the nuget page itself https://www.nuget.org/packages/HtmlAgilityPack/, on the right side, there is a link called "manual download"

The relevant HTML snippet from the Youtube page is as follows. Keep in mind that any changes to Youtube layout will stop this program from 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>

Link to the library website:

http://html-agility-pack.net/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.