Пример #1
0
        /// <summary>
        /// Cтавим в очередь ссылки найденые в поисковиках
        /// </summary>
        private void Start()
        {
            int threadCount = 10;

            while (!_Service.exit)
            {
                foreach (string search in searchers)
                {
                    try
                    {
                        string[] hrefs = WebR.GetYandexHrefs(search);

                        int threads = hrefs.Length >= threadCount ? threadCount : hrefs.Length; //своеобразный threadpool

                        for (int i = 0; i < hrefs.Length; i += threads)                         //перебераем найденые сайты
                        {
                            Sql    sql  = new Sql();
                            Task[] Tsks = new Task[threads];

                            for (int y = 0; y < threads && y + i < hrefs.Length; y++)//разбиваем по потокам
                            {
                                int yi = y + i;
                                Tsks[y] = Task.Run(() =>
                                {
                                    string href          = hrefs[yi];
                                    string[] findedProxy = WebR.GetProxys(href);
                                    Console.WriteLine("created thread - " + href);
                                    foreach (string prx in findedProxy)
                                    {
                                        OnCreate.Add(prx, ref sql);
                                        if (_Service.exit)
                                        {
                                            return;
                                        }
                                    }
                                    Console.WriteLine("dispose thread - " + href);
                                });
                            }

                            Task.WaitAll(Tsks.Where(t => t != null).ToArray());
                            sql.Dispose();
                            if (_Service.exit)
                            {
                                return;
                            }
                        }
                    }
                    catch (Exception e) { Console.WriteLine("finder : " + e.Message); }
                    if (_Service.exit)
                    {
                        return;
                    }
                }
                GC.Collect();
                GC.GetTotalMemory(false);
            }
        }
Пример #2
0
        private async void Start(int threads)
        {
            while (!_Service.exit)
            {
                foreach (string search in searchers)
                {
                    try
                    {
                        string[] hrefs = WebR.GetYandexHrefs(search);

                        List <Task> taskList = new List <Task>();


                        for (int i = 0; i < hrefs.Length; i++)
                        {
                            int yi = i;
                            taskList.Add(Task.Run(() =>
                            {
                                Sql sql = new Sql();
                                if (sql.connectionIsOpen)
                                {
                                    try
                                    {
                                        string href          = hrefs[yi];
                                        string[] findedProxy = WebR.GetProxys(href);
                                        Console.WriteLine("created thread - " + href);
                                        foreach (string prx in findedProxy)
                                        {
                                            OnCreate.Add(prx, ref sql);
                                            if (_Service.exit)
                                            {
                                                return;
                                            }
                                        }
                                    }
                                    catch (Exception e) { Console.WriteLine("one of main threads : " + e.Message); }
                                    finally
                                    {
                                        sql.Dispose();
                                    }
                                }
                            }));

                            //ограничиваем кол-во потоков за раз
                            if (taskList.Where(e => e != null).Count() > threads)
                            {
                                await Task.WhenAny(taskList.ToArray());
                            }



                            if (_Service.exit)
                            {
                                return;
                            }
                        }

                        await Task.WhenAll(taskList.Where(e => e != null).ToArray());

                        GC.Collect();
                        GC.GetTotalMemory(false);
                    }
                    catch (Exception e) { Console.WriteLine("finder : " + e.Message); }
                    if (_Service.exit)
                    {
                        return;
                    }
                }
            }
        }