예제 #1
0
        /// <summary>
        /// The worker method.
        /// </summary>
        /// <param name="task"></param>
        protected override Exception DoTaskWork(ThreadWorkerTaskBase task)
        {
            //if ((Task)task.TaskID == Task.AnonymousDelegate)
            //{
            //    Action action = (Action)task.Arguments[0];
            //    if (action == null)
            //        throw new InvalidOperationException("no Action delegate specified");

            //    action();
            //    // default event if no result(s) from processing:
            //    RaiseBackgroundTaskFinished(task, 1, 1, null, null);
            //    return null;
            //}

            RssBanditApplication app = ((ThreadWorkerTask)task).Application;
            int maxTasks = 0, currentTask = 0;

            bool          force;
            UltraTreeNode feedNode;
            string        stylesheet, html;

            switch ((Task)task.TaskID)
            {
            case Task.LoadAllFeedSourcesSubscriptions:
                List <FeedSourceEntry> entries = (List <FeedSourceEntry>)task.Arguments[0];
                var finished = new ManualResetEvent(false);
                int max      = entries.Count;
                int current  = 0;

                for (int i = 0; i < max; i++)
                {
                    IndexedFeedSourceEntry e = new IndexedFeedSourceEntry(entries[i], i);

                    PriorityThreadPool.QueueUserWorkItem(
                        delegate(object state)
                    {
                        IndexedFeedSourceEntry fs = (IndexedFeedSourceEntry)state;
                        int threadCurrent         = fs.Index + 1;

                        try
                        {
                            app.LoadFeedSourceSubscriptions(fs.Entry, true);
                            this.RaiseBackroundTaskProgress(task, max, threadCurrent, null, fs.Entry);
                        }
                        catch (Exception loadEx)
                        {
                            this.RaiseBackroundTaskProgress(task, max, threadCurrent, loadEx, fs.Entry);
                        }
                        finally
                        {
                            if (Interlocked.Increment(ref current) >= max)
                            {
                                if (finished != null)
                                {
                                    finished.Set();
                                }
                            }
                        }
                    }, e, 1);
                }

                if (max > 0)
                {
                    finished.WaitOne(Timeout.Infinite, true);
                }

                break;

            case Task.LoadFeedSourceSubscriptions:
                app.LoadFeedSourceSubscriptions((FeedSourceEntry)task.Arguments[0], true);
                break;

            // code mocved to FlaggedItemsFeed migrate method:
            //case Task.LoadSpecialFeeds:
            //    app.InitializeFlaggedItems();
            //    break;

            case Task.RefreshFeeds:
                force = (bool)task.Arguments[0];
                app.FeedSources.ForEach(s => s.RefreshFeeds(force));
                break;

            case Task.RefreshCommentFeeds:
                force = (bool)task.Arguments[0];
                app.CommentFeedsHandler.RefreshFeeds(force);
                break;

            case Task.RefreshCategoryFeeds:
                FeedSourceEntry entry    = (FeedSourceEntry)task.Arguments[0];
                string          category = (string)task.Arguments[1];
                force = (bool)task.Arguments[2];
                entry.Source.RefreshFeeds(category, force);
                break;

            case Task.LoadCommentFeed:
                INewsItem item = (INewsItem)task.Arguments[0];
                if (item == null)
                {
                    throw new InvalidOperationException("Non-Null task argument 'item' expected.");
                }

                object result = null;

                if ((item.Feed != null) && (item.Feed.owner is IFacebookFeedSource))
                {
                    IFacebookFeedSource fbSource = item.Feed.owner as IFacebookFeedSource;
                    result = fbSource.GetCommentsForItem(item);
                }
                else
                {
                    NewsFeed cmtFeed = new NewsFeed();
                    cmtFeed.link  = item.CommentRssUrl;
                    cmtFeed.title = item.Feed.title;

                    if (!string.IsNullOrEmpty(item.Feed.authUser))
                    {           // take over credential settings
                        string u = null, p = null;
                        FeedSource.GetFeedCredentials(item.Feed, ref u, ref p);
                        FeedSource.SetFeedCredentials(cmtFeed, u, p);
                    }

                    result = RssParser.DownloadItemsFromFeed(cmtFeed, app.Proxy, FeedSource.Offline);
                }
                RaiseBackgroundTaskFinished(task, maxTasks, currentTask, null, new object[] { result, item, task.Arguments[1], task.Arguments[2] });
                return(null);

            case Task.TransformFeed:
                IFeedDetails feed = (IFeedDetails)task.Arguments[0];
                feedNode   = (UltraTreeNode)task.Arguments[1];
                stylesheet = (string)task.Arguments[2];
                html       = app.FormatFeed(stylesheet, feed);

                RaiseBackgroundTaskFinished(task, maxTasks, currentTask, null,
                                            new object[] { feedNode, html });
                return(null);

            case Task.TransformCategory:
                FeedInfoList feeds = (FeedInfoList)task.Arguments[0];
                feedNode   = (UltraTreeNode)task.Arguments[1];
                stylesheet = (string)task.Arguments[2];

                html = app.FormatFeeds(stylesheet, feeds);

                RaiseBackgroundTaskFinished(task, maxTasks, currentTask, null,
                                            new object[] { feedNode, html });
                return(null);

            default:
                throw new InvalidOperationException("Unhandled ThreadWorker Task: " + task.TaskID);
            }

            // default event if no result(s) from processing:
            RaiseBackgroundTaskFinished(task, maxTasks, currentTask, null, null);
            return(null);
        }