簡體   English   中英

Windows Store應用程序的多線程

[英]Multithreading with Windows Store Applications

我們目前正在創建一個Windows應用商店應用程序,它從RSS提要中獲取信息並將此信息輸入到ObservableCollection中。 我們遇到的問題是,當獲取信息時,應用程序UI變得無法響應。

為了解決這個問題,我想到了創建一個新線程並在其中調用該方法。 雖然經過一些研究后我們意識到在Windows應用商店應用中已不再可能。 我們怎么能解決這個問題呢?

收集信息的方法如下。

public void getFeed()
{
    setupImages();
    string[] feedUrls = new string[] {
        "http://www.igadgetos.co.uk/blog/category/gadget-news/feed/",
        "http://www.igadgetos.co.uk/blog/category/gadget-reviews/feed/",
        "http://www.igadgetos.co.uk/blog/category/videos/feed/",
        "http://www.igadgetos.co.uk/blog/category/gaming/feed/",
        "http://www.igadgetos.co.uk/blog/category/jailbreak-2/feed/",
        "http://www.igadgetos.co.uk/blog/category/kickstarter/feed/",
        "http://www.igadgetos.co.uk/blog/category/cars-2/feed/",
        "http://www.igadgetos.co.uk/blog/category/software/feed/",
        "http://www.igadgetos.co.uk/blog/category/updates/feed/"
    };

    {
        try
        {
            XNamespace dc = "http://purl.org/dc/elements/1.1/";
            XNamespace content = "http://purl.org/rss/1.0/modules/content/";

            foreach (var feedUrl in feedUrls)
            {
                var doc = XDocument.Load(feedUrl);
                var feed = doc.Descendants("item").Select(c => new ArticleItem() //Creates a copy of the ArticleItem Class.
                {
                    Title = c.Element("title").Value,
                    //There are another 4 of these.
                    Post = stripTags(c.Element(content + "encoded").Value)                        }
                ).OrderByDescending(c => c.PubDate);
                this.moveItems = feed.ToList();

                foreach (var item in moveItems)
                {
                    item.ID = feedItems.Count;
                    feedItems.Add(item);
                }
            }
            lastUpdated = DateTime.Now;
        }
        catch
        {
            MessageDialog popup = new MessageDialog("An error has occured downloading the feed, please try again later.");
            popup.Commands.Add(new UICommand("Okay"));
            popup.Title = "ERROR";

            popup.ShowAsync();
        }
    }
}

當我們獲取此信息時,我們如何能夠使應用程序不凍結,因為在Windows應用商店應用程序中無法進行線程處理。

例如 - 我們計划使用;

Thread newThread = new Thread(getFeed);
newThread.Start

您需要為UI線程上發生的操作使用記錄良好的異步模式。 Paul-Jan在評論中給出的鏈接是您需要開始的地方。 http://msdn.microsoft.com/en-us/library/windows/apps/hh994635.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM