Example #1
0
        public static TSave.ConcurrentBag <IEnumerable <long> > scanParallelWithTasks(long startScope, long endScope, DGProgress ProgressCallback)
        {
            var alleTasks = new List <TPL.Task>();

            // Die Ergebnisse werden in dieser Liste abgelegt
            var results = new System.Collections.Concurrent.ConcurrentBag <IEnumerable <long> >();

            // Partitionierung des Auftrages
            for (long start = startScope + 1, ende = startScope + 10000; start < endScope; start += 10000, ende += 10000)
            {
                // Pro Partition wird ein Task aufgesetzt
                var t = new TPL.Task(ParamPartitionAsObject =>
                {
                    // Downcast des Parameters vom Typ Object in den Typ Partition
                    var Arbeitsauftrag = ParamPartitionAsObject as Tuple <long, long>;

                    var result = mko.Algo.NumberTheory.PrimeFactors.scan(Arbeitsauftrag);
                    results.Add(result);

                    // Informieren über die Fertigstellung der Partition
                    if (ProgressCallback != null)
                    {
                        ProgressCallback(Arbeitsauftrag);
                    }
                }, new Tuple <long, long>(start, ende));
                t.Start();

                alleTasks.Add(t);
            }

            // Warten, bis alle Tasks fertiggestellt sind
            TPL.Task.WaitAll(alleTasks.ToArray());

            return(results);
        }
        //startup
        private static List <IPAddress> FindNetworkInterfaces(bool addLoopback = false)
        {
            var ninterfaces      = NetworkInterface.GetAllNetworkInterfaces();
            var activeInterfaces = new System.Collections.Concurrent.ConcurrentBag <IPAddress>();

            Parallel.ForEach <NetworkInterface>(ninterfaces, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 5
            },
                                                iface =>
            {
                if (iface.OperationalStatus == OperationalStatus.Up)
                {
                    var ipprops = iface.GetIPProperties();
                    if (ipprops.UnicastAddresses != null && ipprops.UnicastAddresses.Count > 0)
                    {
                        var ipV4List = ipprops.UnicastAddresses
                                       .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork && x.IsDnsEligible)
                                       .ToList();

                        if (ipV4List.Count > 0)
                        {
                            activeInterfaces.Add(ipV4List.First().Address);
                        }
                    }
                }
            });

            if (addLoopback)
            {
                activeInterfaces.Add(IPAddress.Loopback);
            }
            return(activeInterfaces.ToList());
        }
Example #3
0
        static async public Task <ConcurrentBag <CalendarEvent> > getAllCalEvents(IEnumerable <GoogleTilerEventControl> AllGoogleCalControl, TimeLine CalculationTimeLine, bool retrieveLocationFromGoogle = false)
        {
            ConcurrentBag <List <CalendarEvent> >         RetValueListContainer = new System.Collections.Concurrent.ConcurrentBag <List <CalendarEvent> >();
            ConcurrentBag <Task <List <CalendarEvent> > > ConcurrentTask        = new System.Collections.Concurrent.ConcurrentBag <System.Threading.Tasks.Task <List <TilerElements.CalendarEvent> > >();
            ConcurrentBag <CalendarEvent> RetValue = new System.Collections.Concurrent.ConcurrentBag <CalendarEvent>();

            //AllGoogleCalControl.AsParallel().ForAll(obj=>

            foreach (GoogleTilerEventControl obj in AllGoogleCalControl)
            {
                ConcurrentTask.Add(obj.getCalendarEventsForIndex(CalculationTimeLine, false));
            }
            //);

            /*
             * Parallel.ForEach(ConcurrentTask, async EachTask =>
             *  {
             *      List<CalendarEvent> ALlCalEvents = await EachTask.ConfigureAwait(false);
             *      ALlCalEvents.ForEach(obj1 => RetValue.Add(obj1));
             *  }
             *  );
             */


            foreach (Task <List <CalendarEvent> > EachTask in ConcurrentTask)
            {
                List <CalendarEvent> ALlCalEvents = await EachTask.ConfigureAwait(false);

                ALlCalEvents.ForEach(obj1 => RetValue.Add(obj1));
            }

            return(RetValue);
        }
Example #4
0
        private List <FingerprintAcoustID> GetFingerprintsMySQL(int[] fingerIDList)
        {
            DateTime startTime = DateTime.Now;

            System.Collections.Concurrent.ConcurrentBag <FingerprintAcoustID> fingerBag = new System.Collections.Concurrent.ConcurrentBag <FingerprintAcoustID>();

            using (MySql.Data.MySqlClient.MySqlConnection conn = CDR.DB_Helper.NewMySQLConnection())
            {
                StringBuilder sb = new StringBuilder(1024);
                sb.Append("SELECT *\r\n");
                sb.Append("FROM   FINGERID AS T1,\r\n");
                sb.Append("       TITELNUMMERTRACK_ID AS T2\r\n");
                sb.Append("WHERE  T1.TITELNUMMERTRACK_ID = T2.TITELNUMMERTRACK_ID\r\n");
                sb.Append("AND    T2.TITELNUMMERTRACK_ID IN (\r\n");

                int count = 0;
                System.Collections.Hashtable hTable = new System.Collections.Hashtable(fingerIDList.Length);
                foreach (int id in fingerIDList)
                {
                    if (count > 0)
                    {
                        sb.Append(',');
                    }
                    sb.Append(id.ToString());
                    hTable.Add(id, count);
                    count++;
                }
                sb.Append(')');

                MySql.Data.MySqlClient.MySqlCommand command = new MySql.Data.MySqlClient.MySqlCommand(sb.ToString(), conn);
                command.CommandTimeout = 60;

                MySql.Data.MySqlClient.MySqlDataAdapter adapter = new MySql.Data.MySqlClient.MySqlDataAdapter(command);
                System.Data.DataSet ds = new System.Data.DataSet();
                adapter.Fill(ds);
                if (ds.Tables.Count > 0)
                {
                    foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                    {
                        FingerprintAcoustID fs = new FingerprintAcoustID();
                        fs.Reference    = row["TITELNUMMERTRACK"].ToString();
                        fs.Signature    = (byte[])row["SIGNATURE"];
                        fs.DurationInMS = Convert.ToInt64(row["DURATIONINMS"]);

                        int titelnummertrackID = Convert.ToInt32(row["TITELNUMMERTRACK_ID"]);
                        fs.Tag = hTable[titelnummertrackID];

                        fingerBag.Add(fs);
                    }
                }
            }

            List <FingerprintAcoustID> result = fingerBag.OrderBy(e => (int)e.Tag)
                                                .ToList();

            return(result);
        }
Example #5
0
        /// <summary>
        /// Liefert eine Liste aller Primzahlen in einem Intervall [begin, end].
        /// Die Suche wird mittels der Task Parallel Library auf einem Multicore- System
        /// durch Aufteilen in kleine Suchbereiche und absuchen dieser in jeweils einem Thread
        /// beschleunigt.
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static IEnumerable <long> scanParallel(long begin, long end, bool orderByDesc)
        {
            // Suchbereich in Teilbereiche aufteilen, die parallel nach Primzahlen durchsucht werden
            var partitionen = System.Collections.Concurrent.Partitioner.Create(begin, end);

            // Liste der Teilergebnisse. Die ConcurrentBag ist threadsafe !
            var results = new System.Collections.Concurrent.ConcurrentBag <IEnumerable <long> >();

            // Paralleles starten aller Suchaufträge
            TPL.Parallel.ForEach(partitionen, (part) =>
            {
                results.Add(scan(part));
                Debug.WriteLine("results.Count = " + results.Count);
            });

            // Zusammenführen der Resultate der parallelen Suchaufträge
            IEnumerable <long> all = null;

            foreach (var part in results)
            {
                if (all == null)
                {
                    all = part;
                }
                else
                {
                    all = all.Concat(part);
                }
            }
            if (all != null)
            {
                // Nachbearbeitung der Ergebnisses
                if (orderByDesc)
                {
                    return(all.OrderByDescending(p => p).ToArray());
                }
                else
                {
                    return(all.OrderBy(p => p).ToArray());
                }
            }
            else
            {
                return new long[] { }
            };
        }
    }
Example #6
0
        /// <summary>
        /// Liefert eine Liste aller Primzahlen in einem Intervall [begin, end].
        /// Die Suche wird mittels der Task Parallel Library auf einem Multicore- System
        /// durch Aufteilen in kleine Suchbereiche und absuchen dieser in jeweils einem Thread
        /// beschleunigt.
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public static TSave.ConcurrentBag <IEnumerable <long> > scanParallelWithParalleForEach(long begin, long end, DGProgress ProgressCallback)
        {
            // Suchbereich in Teilbereiche aufteilen, die parallel nach Primzahlen durchsucht werden
            var partitionen = System.Collections.Concurrent.Partitioner.Create(begin, end);

            // Liste der Teilergebnisse. Die ConcurrentBag ist threadsafe !
            var results = new System.Collections.Concurrent.ConcurrentBag <IEnumerable <long> >();

            // Paralleles starten aller Suchaufträge
            TPL.Parallel.ForEach(partitionen, (part) =>
            {
                results.Add(scan(part));

                // Informieren über die Fertigstellung der Partition
                Debug.WriteLine("results.Count = " + results.Count);

                if (ProgressCallback != null)
                {
                    ProgressCallback(part);
                }
            });

            return(results);
        }
        public async Task ConnectAsync(string broadcasterId, int cnum, long live_id)
        {
            _first.Reset();
            _cts             = new CancellationTokenSource();
            _receivedItemIds = new System.Collections.Concurrent.ConcurrentBag <string>();
            //TODO:try-catch
            //var liveInfo = await API.GetLiveContext(_server, broadcasterId);
            //var cnum = liveInfo.MovieCnum;
            //var live_id = liveInfo.MovieId;
            long lastCommentId = 0;

            try
            {
                var(initialComments, initialRaw) = await API.GetListAll(_server, broadcasterId, live_id, lastCommentId, 0, 20, _cc);

                if (initialComments.Length > 0)
                {
                    foreach (var lowComment in initialComments)
                    {
                        //showがfalseのデータが時々ある。
                        //{"id":15465669455,"show":false}
                        //よく分からないけど、有用な情報は無さそうだからスルー
                        if (!lowComment.show)
                        {
                            continue;
                        }

                        var context = CreateMessageContext(lowComment, true, initialRaw);
                        MessageReceived?.Invoke(this, context);
                    }
                    //var initialDataList = LowComment2Data(initialComments, initialRaw);
                    //if (initialDataList.Count > 0)
                    //{
                    //    InitialCommentsReceived?.Invoke(this, initialDataList);
                    //}
                    var lastComment = initialComments[initialComments.Length - 1];
                    lastCommentId = lastComment.id;
                }
            }
            catch (HttpRequestException ex)
            {
                _logger.LogException(ex);
                string message;
                if (ex.InnerException != null)
                {
                    message = ex.InnerException.Message;
                }
                else
                {
                    message = ex.Message;
                }
                SendInfo(message, InfoType.Debug);
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
                SendInfo(ex.Message, InfoType.Debug);
            }
            //Disconnect()が呼ばれた場合以外は接続し続ける。
            while (!_cts.IsCancellationRequested)
            {
                var    waitTimeMs  = 1000 * _siteOptions.CommentRetrieveIntervalSec;
                var    accWaitTime = 0;
                string lastItemId  = null;
                try
                {
                    var(streamChecker, streamCheckerRaw) = await API.GetUtreamChecker(_server, broadcasterId, lastItemId).ConfigureAwait(false);

                    if (streamChecker.Items != null && streamChecker.Items.Count > 0)
                    {
#if DEBUG
                        try
                        {
                            using (var sw = new System.IO.StreamWriter("アイテムあり.txt", true))
                            {
                                sw.WriteLine(streamCheckerRaw);
                            }
                        }
                        catch (Exception) { }
#endif
                        var lastItem         = streamChecker.Items[streamChecker.Items.Count - 1];
                        var lastItemIdBefore = lastItemId == null ? 0 : long.Parse(lastItemId);
                        lastItemId = Math.Max(lastItemIdBefore, long.Parse(lastItem.Id)).ToString();
                    }
                    MetaReceived?.Invoke(this, new Metadata
                    {
                        Title          = streamChecker.Telop,
                        CurrentViewers = streamChecker.CurrentViewers.ToString(),
                        TotalViewers   = streamChecker.TotalViewers.ToString()
                    });
                    foreach (var item in streamChecker.Items)
                    {
                        try
                        {
                            if (_receivedItemIds.Contains(item.Id))
                            {
                                continue;
                            }

                            ITwicasItem itemMessage = null;
                            if (Tools.IsKiitos(item))
                            {
                                itemMessage = Tools.CreateKiitosMessage(item);
                            }
                            else
                            {
                                var image = new MessageImage
                                {
                                    Url    = item.ItemImage,
                                    Alt    = item.t13,
                                    Height = 40,
                                    Width  = 40,
                                };
                                itemMessage = new TwicasItem(item.Raw)
                                {
                                    UserIcon = new MessageImage
                                    {
                                        Url    = item.SenderImage,
                                        Alt    = item.t13,
                                        Height = 40,
                                        Width  = 40,
                                    },
                                    ItemName = item.t13,
                                    //CommentItems = new List<IMessagePart> { Common.MessagePartFactory.CreateMessageText(item.t13) },
                                    CommentItems = new List <IMessagePart> {
                                        image
                                    },
                                    NameItems = new List <IMessagePart> {
                                        Common.MessagePartFactory.CreateMessageText(item.t12)
                                    },
                                    UserId = item.SenderName,
                                    ItemId = item.Id,
                                };
                            }
                            if (itemMessage != null)
                            {
                                var user     = GetUser(item.SenderName);
                                var metadata = new MessageMetadata(itemMessage, _options, _siteOptions, user, _cp, false)
                                {
                                    IsInitialComment = false,
                                    SiteContextGuid  = SiteContextGuid,
                                };
                                var methods = new TwicasMessageMethods();
                                var context = new TwicasMessageContext(itemMessage, metadata, methods);
                                MessageReceived?.Invoke(this, context);
                            }
                            SendInfo(item.SenderName + " " + item.ItemImage, InfoType.Debug);
                            _receivedItemIds.Add(item.Id);
                        }
                        catch (ParseException ex)
                        {
                            _logger.LogException(ex);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogException(ex);
                        }
                    }

                    if (streamChecker.LiveId == null)
                    {
                        //放送してない。live_idは更新しない。
                    }
                    else
                    {
                        live_id = streamChecker.LiveId.Value;
                        SendInfo($"Twicas live_id={live_id}", InfoType.Debug);
                    }
                    var(lowComments, newCnum, updateRaw) = await API.GetListUpdate(_server, broadcasterId, live_id, cnum, lastCommentId, _cc);

                    if (lowComments != null && lowComments.Count > 0)
                    {
                        cnum = newCnum;


                        lastCommentId = lowComments[lowComments.Count - 1].id;
                        var eachInterval = waitTimeMs / lowComments.Count;
                        foreach (var lowComment in lowComments)
                        {
                            //showがfalseのデータが時々ある。
                            //{"id":15465669455,"show":false}
                            //よく分からないけど、有用な情報は無さそうだからスルー
                            if (!lowComment.show)
                            {
                                continue;
                            }

                            var context = CreateMessageContext(lowComment, false, updateRaw);
                            MessageReceived?.Invoke(this, context);

                            await Task.Delay(eachInterval);

                            accWaitTime += eachInterval;
                        }
                        ////htmlが""のことがある。コメントを削除した?省いておく
                        //var dataCollection = LowComment2Data(lowComments, updateRaw);//.Where(s=>!string.IsNullOrEmpty(s.html)).Select(Tools.Parse).ToList();
                        //if (dataCollection.Count > 0)
                        //{
                        //    lastCommentId = dataCollection[dataCollection.Count - 1].Id;

                        //    var eachInterval = waitTimeMs / dataCollection.Count;
                        //    foreach (var data in dataCollection)
                        //    {
                        //        Received?.Invoke(this, new List<ICommentData> { data });

                        //        await Task.Delay(eachInterval);
                        //        accWaitTime += eachInterval;
                        //    }
                        //}
                    }
                }
                catch (HttpRequestException ex)
                {
                    _logger.LogException(ex);
                    string message;
                    if (ex.InnerException != null)
                    {
                        message = ex.InnerException.Message;
                    }
                    else
                    {
                        message = ex.Message;
                    }
                    SendInfo(message, InfoType.Debug);
                }
                catch (ParseException ex)
                {
                    _logger.LogException(ex);
                    SendInfo(ex.Message, InfoType.Debug);
                }
                catch (TaskCanceledException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    _logger.LogException(ex);
                    //Infoでエラー内容を通知。ただし同じエラーが連続する場合は通知しない
                    SendInfo(ex.Message, InfoType.Debug);
                }
                try
                {
                    var restWait = waitTimeMs - accWaitTime;
                    if (restWait > 0)
                    {
                        await Task.Delay(restWait, _cts.Token);
                    }
                }
                catch (TaskCanceledException)
                {
                    break;
                }
            }
            _cts = null;
        }