Example #1
0
        private void UpdateStep(int step, bool action, bool clear = false, byte insideThreadCount = 3)
        {
            if (clear)
                UrlsToAddList.Clear();

            #region step 1
            if (step == 1 && action)
            {
                object lockAdd = new Object();
                UrlsToAddList.Clear();

#if DEBUG
                insideThreadCount = 1;
#else
                insideThreadCount = ThreadCount;
#endif

                ParseRuleConnectionType type = NewParseRule.Connection;

                int minWidth = NewParseRule.MinImageWidth;
                int minHeight = NewParseRule.MinImageHeight;
                bool collectIMGTags = NewParseRule.CollectIMGTags;
                bool collectLINKTags = NewParseRule.CollectLINKTags;
                bool collectMETATags = NewParseRule.CollectMETATags;
                byte threadCount = this.ThreadCount;

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (s, e) =>
                    {
                        Helpers.PercentageProgress progress = new Helpers.PercentageProgress();
                        progress.Change += (sP, eP) =>
                            {
                                bw.ReportProgress((int)eP.Value);
                            };

                        List<UrlResultWrapper> urlResultWrapper = new List<UrlResultWrapper>();
                        var urls = e.Argument as StringUrlWithResultWrapper[];

                        if (urls != null)
                            urls
                                .Where(item => item != null && !string.IsNullOrWhiteSpace(item.Value) && Helper.IsWellFormedUriString(item.Value, UriKind.Absolute))
                                .Select(sw => 
                                    new 
                                        { 
                                            item = sw,
                                            prgItem = progress.GetChild() 
                                        })
                                .ToArray()
                                .AsParallel()
                                .WithDegreeOfParallelism(insideThreadCount)
                                .ForAll(
                                    (sw) =>
                                    {
                                        var item = new UrlResultWrapper() { Value = sw.item.Value };

                                        System.Drawing.Size minSize = new System.Drawing.Size() { Width = minWidth, Height = minHeight };

                                        var result = Helper.GetAllImagesFromUrl(item.Value, minSize, collectIMGTags, collectLINKTags, collectMETATags, threadCount, sw.prgItem, true, type);

                                        foreach (ParseImageResult res in result)
                                            item.ParseResult.Add(res);

                                        if (item.ParseResult.Count > 0)
                                            lock (lockAdd)
                                            {
                                                urlResultWrapper.Add(item);
                                            }
                                    });

                        e.Result = urlResultWrapper;
                    };
                bw.RunWorkerCompleted += (s, e) =>
                    {
                        if (e.Error != null)
                            throw e.Error;

                        try
                        {
                            List<UrlResultWrapper> urlResultWrapper = e.Result as List<UrlResultWrapper>;
                            foreach (var item in urlResultWrapper)
                            {
                                if (item.ParseResult != null)
                                    foreach (var ps in item.ParseResult)
                                        ps.IsSelected = (item.ParseResult.IndexOf(ps) == 0);
                                
                                UrlsToAddList.Add(item);
                            }
                        }
                        finally
                        {
                            bw.Dispose();
                            IsBusy = false;
                        }
                    };
                bw.WorkerReportsProgress = true;
                bw.ProgressChanged += (s, e) =>
                    {
                        LoadedPercent = e.ProgressPercentage;
                    };
                IsBusy = true;
                bw.RunWorkerAsync(Urls.ToArray());
                while (bw.IsBusy)
                    Helper.DoEvents();
                bw = null;
            }
#endregion
#region step 2
            else if (step == 2 && action)
            {
                HtmlNodeWithUrl[] nodes =
                    UrlsToAddList
                    .Where(n => !string.IsNullOrWhiteSpace(n.Value))
                    .Select(i => 
                        {
                            ParseImageResult res = i.ParseResult.Where(i2 => i2.IsSelected).FirstOrDefault();
                            return
                                new HtmlNodeWithUrl()
                                {
                                    Node = res == null ? null : res.Node,
                                    Url = res == null ? new Uri(i.Value, UriKind.RelativeOrAbsolute) : res.Url
                                };
                        }
                    )
                    .Where(i3 => i3 != null && i3.Node != null)
                    .ToArray();

                ParseRule newRule = Helper.GetRule(nodes, NewParseRule.Label, NewParseRule.MinImageSize, NewParseRule.CollectIMGTags, NewParseRule.CollectLINKTags, NewParseRule.CollectMETATags);
                newRule.CopyObject(NewParseRule, new string[] { "Connection" });

                ShowRuleModeCommand.Execute(null);
            }
#endregion

            if (step >= UrlsToAddTabControl.Items.Count)
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == UrlsToAddTabControl.Items.Count - 1) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            else if (step < 0)
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == 0) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            else
            { 
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == step) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }

            UrlsToAddTabControl.SelectedIndex = UrlsToAddTabControl.Items.IndexOf(UrlsToAddTabControl.Items.Cast<TabItem>().FirstOrDefault(ti => ti.Visibility == System.Windows.Visibility.Visible));
        }
        private void UpdateStep(int step, bool action, bool clear = false, byte insideThreadCount = 3)
        {
            if (clear)
                UrlsToAddList.Clear();

            #region step 1
            if (step == 1 && action)
            {
                object lockAdd = new Object();
                UrlsToAddList.Clear();

            #if DEBUG
                insideThreadCount = 1;
            #else
                insideThreadCount = ThreadCount;
            #endif

                ParseRuleConnectionType type = NewParseRule.Connection;

                int minWidth = NewParseRule.MinImageWidth;
                int minHeight = NewParseRule.MinImageHeight;
                bool collectIMGTags = NewParseRule.CollectIMGTags;
                bool collectLINKTags = NewParseRule.CollectLINKTags;
                bool collectMETATags = NewParseRule.CollectMETATags;
                byte threadCount = this.ThreadCount;

                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (s, e) =>
                    {
                        Helpers.PercentageProgress progress = new Helpers.PercentageProgress();
                        progress.Change += (sP, eP) =>
                            {
                                bw.ReportProgress((int)eP.Value);
                            };

                        List<UrlResultWrapper> urlResultWrapper = new List<UrlResultWrapper>();
                        var urls = e.Argument as StringUrlWithResultWrapper[];

                        if (urls != null)
                            urls
                                .Where(item => item != null && !string.IsNullOrWhiteSpace(item.Value) && Helper.IsWellFormedUriString(item.Value, UriKind.Absolute))
                                .Select(sw =>
                                    new
                                        {
                                            item = sw,
                                            prgItem = progress.GetChild()
                                        })
                                .ToArray()
                                .AsParallel()
                                .WithDegreeOfParallelism(insideThreadCount)
                                .ForAll(
                                    (sw) =>
                                    {
                                        var item = new UrlResultWrapper() { Value = sw.item.Value };

                                        System.Drawing.Size minSize = new System.Drawing.Size() { Width = minWidth, Height = minHeight };

                                        var result = Helper.GetAllImagesFromUrl(item.Value, minSize, collectIMGTags, collectLINKTags, collectMETATags, threadCount, sw.prgItem, true, type);

                                        foreach (ParseImageResult res in result)
                                            item.ParseResult.Add(res);

                                        if (item.ParseResult.Count > 0)
                                            lock (lockAdd)
                                            {
                                                urlResultWrapper.Add(item);
                                            }
                                    });

                        e.Result = urlResultWrapper;
                    };
                bw.RunWorkerCompleted += (s, e) =>
                    {
                        if (e.Error != null)
                            throw e.Error;

                        try
                        {
                            List<UrlResultWrapper> urlResultWrapper = e.Result as List<UrlResultWrapper>;
                            foreach (var item in urlResultWrapper)
                            {
                                if (item.ParseResult != null)
                                    foreach (var ps in item.ParseResult)
                                        ps.IsSelected = (item.ParseResult.IndexOf(ps) == 0);

                                UrlsToAddList.Add(item);
                            }
                        }
                        finally
                        {
                            bw.Dispose();
                            IsBusy = false;
                        }
                    };
                bw.WorkerReportsProgress = true;
                bw.ProgressChanged += (s, e) =>
                    {
                        LoadedPercent = e.ProgressPercentage;
                    };
                IsBusy = true;
                bw.RunWorkerAsync(Urls.ToArray());
                while (bw.IsBusy)
                    Helper.DoEvents();
                bw = null;
            }
            #endregion
            #region step 2
            else if (step == 2 && action)
            {
                HtmlNodeWithUrl[] nodes =
                    UrlsToAddList
                    .Where(n => !string.IsNullOrWhiteSpace(n.Value))
                    .Select(i =>
                        {
                            ParseImageResult res = i.ParseResult.Where(i2 => i2.IsSelected).FirstOrDefault();
                            return
                                new HtmlNodeWithUrl()
                                {
                                    Node = res == null ? null : res.Node,
                                    Url = res == null ? new Uri(i.Value, UriKind.RelativeOrAbsolute) : res.Url
                                };
                        }
                    )
                    .Where(i3 => i3 != null && i3.Node != null)
                    .ToArray();

                ParseRule newRule = Helper.GetRule(nodes, NewParseRule.Label, NewParseRule.MinImageSize, NewParseRule.CollectIMGTags, NewParseRule.CollectLINKTags, NewParseRule.CollectMETATags);
                newRule.CopyObject(NewParseRule, new string[] { "Connection" });

                ShowRuleModeCommand.Execute(null);
            }
            #endregion

            if (step >= UrlsToAddTabControl.Items.Count)
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == UrlsToAddTabControl.Items.Count - 1) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            else if (step < 0)
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == 0) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            else
            {
                for (int i = UrlsToAddTabControl.Items.Count - 1; i >= 0; i--)
                    (UrlsToAddTabControl.Items[i] as TabItem).Visibility = (i == step) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }

            UrlsToAddTabControl.SelectedIndex = UrlsToAddTabControl.Items.IndexOf(UrlsToAddTabControl.Items.Cast<TabItem>().FirstOrDefault(ti => ti.Visibility == System.Windows.Visibility.Visible));
        }