Esempio n. 1
0
        /// <summary>
        /// Request the collection to be filled up to the requested <paramref name="count"/>, if possible.
        /// </summary>
        /// <param name="count">The count requested.</param>
        public async void Request(int count)
        {
            void AddCallback(object state) => Add((T)state);

            void WorkingCallback(object state) => Working?.Invoke(this, (bool)state);

            // If already satisfied, skip this one.
            if (Count > count)
            {
                return;
            }

            // Queue a new resolution.
            await Task.Run(async() =>
            {
                // Allow only one working thread.
                await _lock.WaitAsync();

                try
                {
                    // Check again in task, another task might have already generated the necessary elements.
                    if (Count > count)
                    {
                        return;
                    }

                    // Mark as start working on context.
                    _syncOn.Send(WorkingCallback, true);

                    // While elements need to be filled and available, add on context.
                    while (Count < count && await _generator.MoveNextAsync())
                    {
                        _syncOn.Send(AddCallback, _generator.Current);
                    }
                }
                catch (OperationCanceledException)
                {
                    // Ok, can occur if underlying enumerator is cancelled.
                }
                finally
                {
                    // Mark as done working on context.
                    _syncOn.Send(WorkingCallback, false);

                    // Release lock.
                    _lock.Release();
                }
            });
        }
Esempio n. 2
0
        public async Task <IEnumerable <ParserResult> > RunAsync(params ParserObject[] objects)
        {
            var resultList = new List <ParserResult>();

            foreach (var item in objects)
            {
                ParserResult result = await RunAsync(item);

                resultList.Add(result);

                Working?.Invoke(this, result.Hedar);
            }

            return(resultList);
        }
Esempio n. 3
0
 protected virtual void InvokeWorking(string e) => Working?.Invoke(this, e);
Esempio n. 4
0
 public void TimeWorking(int i)
 {
     Time = Working.Invoke(i);
     Console.WriteLine($"Пользователь провел сегодня за компьютером: {Time} часов");
 }