private const int GameCacheDuration = 43200; // 12 hours

        public async Task<GameDetails[]> ParallelLoadGames(IEnumerable<int> gameIds)
        {
            GameDetails[] results;
            var tasks = new List<Task<GameDetails>>();
            using (var throttler = new SemaphoreSlim(5))
            {
                foreach (var gameId in gameIds)
                {
                    await throttler.WaitAsync();
                    tasks.Add(Task<GameDetails>.Run(async () =>
                    {
                        try
                        {
                            Debug.WriteLine("Loading {0}...", gameId);
                            return await this.LoadGame(gameId, true);
                        }
                        finally
                        {
                            Debug.WriteLine("Done with {0}...", gameId);
                            throttler.Release();
                        }
                    }));

                }
                results = await Task.WhenAll(tasks);
            }
            return results;
        }
        public async Task DownloadImages(string mainWallpaperDir, List<PWImage> imageList, Action<PWImage, string> SetupProgress, TaskScheduler scheduler)
        {
            var downloads = new List<Task>();

            SemaphoreSlim semaphore = new SemaphoreSlim(15);
            foreach (var image in imageList)
            {
                await semaphore.WaitAsync();
                var localImage = image;
                var path = Path.Combine(mainWallpaperDir, localImage.Theme.Name, localImage.imageName);
                

                downloads.Add(DownloadImage(image, path, SetupProgress, () => semaphore.Release(), scheduler));

            }
            if(downloads.Any())
                await Task.WhenAll(downloads);
        }
Beispiel #3
0
        public async Task <object> RoundtripMethodCallAsync(string connectionId, string methodName, MethodRequestAndResponse requestAndResponse)
        {
            Console.WriteLine("RoundtripMethodCallAsync received for {0} and methodName {1}", connectionId, methodName);
            Console.WriteLine(JsonConvert.SerializeObject(requestAndResponse));
            var client = objectMap[connectionId];
            var mutex  = new System.Threading.SemaphoreSlim(1);
            await mutex.WaitAsync().ConfigureAwait(false);  // Grab the mutex. The handler will release it later

            MethodCallback callback = async(methodRequest, userContext) =>
            {
                Console.WriteLine("Method invocation received");

                object request  = JsonConvert.DeserializeObject(methodRequest.DataAsJson);
                string received = JsonConvert.SerializeObject(new JRaw(request));
                string expected = ((Newtonsoft.Json.Linq.JToken)requestAndResponse.RequestPayload)["payload"].ToString();
                Console.WriteLine("request expected: " + expected);
                Console.WriteLine("request received: " + received);
                if (expected != received)
                {
                    Console.WriteLine("request did not match expectations");
                    Console.WriteLine("Releasing the method mutex");
                    mutex.Release();
                    return(new MethodResponse(500));
                }
                else
                {
                    int status = 200;
                    if (requestAndResponse.StatusCode != null)
                    {
                        status = (int)requestAndResponse.StatusCode;
                    }

                    byte[] responseBytes = GlueUtils.ObjectToBytes(requestAndResponse.ResponsePayload);

                    Console.WriteLine("Releasing the method mutex");
                    mutex.Release();

                    Console.WriteLine("Returning the result");
                    return(new MethodResponse(responseBytes, status));
                }
            };

            Console.WriteLine("Setting the handler");
            await client.SetMethodHandlerAsync(methodName, callback, null).ConfigureAwait(false);

            Console.WriteLine("Waiting on the method mutex");
            await mutex.WaitAsync().ConfigureAwait(false);

            Console.WriteLine("Method mutex released.  Waiting for a tiny bit.");  // Otherwise, the connection might close before the response is actually sent
            await Task.Delay(100).ConfigureAwait(false);

            Console.WriteLine("Nulling the handler");
            await client.SetMethodHandlerAsync(methodName, null, null).ConfigureAwait(false);

            Console.WriteLine("RoundtripMethodCallAsync is complete");
            return(new object());
        }
		public async Task SemaphoreSlimLock()
		{
			var _lock = new SemaphoreSlim( 1 );
			await _lock.WaitAsync();
			_lock.Release();

			var start = Stopwatch.GetTimestamp();
			for( var i = 0; i < Iterations; i++ ) {
				await _lock.WaitAsync();
				_lock.Release();
					i--;
					i++;
			}
			ReportTime( start );
		}
Beispiel #5
0
    public static async Task <T> DeserializeAsync <T>(Windows.Storage.StorageFile f, System.Threading.SemaphoreSlim semaphore) where T : class
    {
        await semaphore.WaitAsync();

        try
        {
            if (f is null)
            {
                return(null);
            }
            using var s1 = await f.OpenAsync(Windows.Storage.FileAccessMode.Read);

            using var s2 = s1.AsStream();
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            return(serializer.Deserialize(s2) as T);
        }
        catch
        {
            return(null);
        }
        finally
        {
            semaphore.Release();
        }
    }
Beispiel #6
0
        // WIP, not producing useful numbers yet. Assumes one partition.
        public static async Task<long> Consume(string broker, string topic)
        {
            long n = 0;

            var topicConfig = new TopicConfig();
            topicConfig["auto.offset.reset"] = "smallest";
            var config = new Config()
            {
                GroupId = "benchmark-consumer",
                DefaultTopicConfig = topicConfig
            };
            using (var consumer = new EventConsumer(config, broker))
            {
                var signal = new SemaphoreSlim(0, 1);

                consumer.OnMessage += (obj, msg) =>
                {
                    n += 1;
                };

                consumer.OnEndReached += (obj, end) =>
                {
                    Console.WriteLine($"End reached");
                    signal.Release();
                };

                consumer.Subscribe(new List<string>{topic});
                consumer.Start();

                await signal.WaitAsync();
                Console.WriteLine($"Shutting down");
            }

            return n;
        }
Beispiel #7
0
 public async Task<AuthorizationResult> AcquireAuthorizationAsync(Uri authorizationUri, Uri redirectUri, CallState callState)
 {
     returnedUriReady = new SemaphoreSlim(0);
     Authenticate(authorizationUri, redirectUri, callState);
     await returnedUriReady.WaitAsync();
     return authorizationResult;
 }
Beispiel #8
0
    static int Main(string[] args)
    {
        SemaphoreSlim s = new SemaphoreSlim(initialCount: 1);

        var cts = new CancellationTokenSource();
        s.Wait();
        var t = s.WaitAsync(cts.Token);
        s.Release();
        cts.Cancel();


        if (t.Status != TaskStatus.Canceled && s.CurrentCount == 0)
        {
            Console.WriteLine("PASS");
            return 100;
        }
        else
        {
            Console.WriteLine("FAIL");
            Console.WriteLine("Expected task status to not be Canceled and s.CurrentCount == 0");
            Console.WriteLine("Actual: Task: " + t.Status + "; CurrentCount: " + s.CurrentCount);
            return 101;
        }


    }
Beispiel #9
0
        internal async Task Update(IEnumerable <string> streams, Func <Task <Metadata> > fetchHandler)
        {
            await Semaphore.WaitAsync();

            var newMetadata = await fetchHandler();

            // Updates all streams
            if (!streams.Any())
            {
                MetadataData = newMetadata;
            }
            // Updates only specified streams
            else
            {
                var updatedStreams = MetadataData.Streams;

                // Removes streams from the cache that are not found in the cluster
                foreach (var stream in streams.Where(s => !newMetadata.HasStreamInfo(s)))
                {
                    updatedStreams = updatedStreams.Remove(stream);
                }
                // Update the stream info for existing streams
                foreach (var stream in streams.Where(s => newMetadata.HasStreamInfo(s)))
                {
                    updatedStreams = updatedStreams.SetItem(stream, newMetadata.Streams[stream]);
                }
                MetadataData = newMetadata with {
                    Streams = updatedStreams
                };
            }
            Semaphore.Release();
        }
Beispiel #10
0
        public void Execute(IJobExecutionContext context) {
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            Logger.Info("开始抓取一号店数据");

            var downloadedCategories = new YhdCategoryExtractor().Extract().Result;
            var needProcessCategories = _CategoryArchiveService.Archive(downloadedCategories).OrderBy(c => c.ProductsUpdateTime);
            
            var taskLock = new SemaphoreSlim(initialCount: 1);
            var tasks = needProcessCategories.Select(async (category, index) => {
                await taskLock.WaitAsync();
                try {
                    var result = await ProcessCategoryAsync(category);
                    //■◆▲●□◇△○
                    Logger.Info(string.Join(" ", new[]{
                            string.Format("{0}", index + 1),
                            string.Format("[{0}]{1}", category.Number, category.Name),
                            string.Format("□{0} △{1}", result.Total, result.Changed)
                        }));
                }
                catch (Exception e) {
                    Logger.ErrorFormat("处理分类{0}{1}失败", e, category.Name, category.Number);
                }
                finally {
                    taskLock.Release();
                }
            });

            Task.WaitAll(tasks.ToArray());

            Logger.InfoFormat("抓取一号店数据完成,用时{0:0.#}分", stopwatch.Elapsed.TotalMinutes);
        }
Beispiel #11
0
        private async Task <(int, int)> Scrape(Func <CancellationToken, Task <int> > process, System.Threading.SemaphoreSlim complete, TimeSpan interval, CancellationToken cancellation)
        {
            var success = 0;
            var error   = 0;
            var delay   = interval;

            while (!cancellation.IsCancellationRequested)
            {
                if (await complete.WaitAsync(delay, cancellation))
                {
                    complete.Release();
                    break;
                }
                try
                {
                    var start = DateTimeOffset.UtcNow;
                    await process(cancellation);

                    var elapsed = DateTimeOffset.UtcNow - start;
                    delay = elapsed > interval ? TimeSpan.Zero : (interval - elapsed);
                    ++success;
                }
                catch { ++error; }
            }

            return(success, error);
        }
        public async Task<TokenPair> Authorize(string clientId, string clientSecret, IEnumerable<string> scopes)
        {
            string uri = string.Format("/LoginPage.xaml?authEndpoint={0}&clientId={1}&scope={2}",
                                       authEndpoint,
                                       clientId,
                                       string.Join(" ", scopes));

            SemaphoreSlim semaphore = new SemaphoreSlim(0, 1);

            Observable.FromEvent<NavigatingCancelEventHandler, NavigatingCancelEventArgs>(
                h => new NavigatingCancelEventHandler(h),
                h => this.frame.Navigating += h,
                h => this.frame.Navigating -= h)
                      .SkipWhile(h => h.EventArgs.NavigationMode != NavigationMode.Back)
                      .Take(1)
                      .Subscribe(e => semaphore.Release());

            frame.Navigate(new Uri(uri, UriKind.Relative));

            await semaphore.WaitAsync();

            string authorizationCode = (string)PhoneApplicationService.Current.State["OAuth_Demo.AuthorizationCode"];

            return await RequestAccessToken(authorizationCode, clientId, clientSecret);
        }
        private async Task TestCancelWaitAsyncInternal()
        {
            var r = new Random();
            var s = new SemaphoreSlim(1);

            await Task.WhenAll(Enumerable.Range(0, 100).Select(async i =>
            {
                var ct = CancellationToken.None;
                if ((i % 5) == 0)
                {
                    var cts = new CancellationTokenSource();
                    var t = Task.Delay(1).ContinueWith(_ => cts.Cancel());
                    ct = cts.Token;
                }

                try
                {
                    await s.WaitAsync(ct);
                    await Delay(r, ct).ConfigureAwait(false);
                }
                catch (TestException) { }
                catch (OperationCanceledException) { }
                finally
                {
                    s.Release();
                }
            }));
        }
Beispiel #14
0
        // TODO:
        // Hardcode a time out value for cancellation token.
        // For some time consuming operations, this time out needs to be tuned.
        public static Task BatchProcess <T>(IList <T> source, Func <T, Task> f, int max)
        {
            var initial = (max >> 1);
            var s       = new System.Threading.SemaphoreSlim(initial, max);

            _ = Task.Run(async() =>
            {
                for (int i = initial; i < max; i++)
                {
                    await Task.Delay(100);
                    s.Release();
                }
            });

            return(Task.WhenAll(from item in source
                                select Task.Run(async() =>
            {
                await s.WaitAsync();
                try
                {
                    await f(item);
                }
                catch (Exception e)
                {
                    Log.Warning($"see exception in {f.Method.Name}: {e.Message}");
                }
                finally
                {
                    s.Release();
                }
            })));
        }
 public async Task<AuthorizationResult> AcquireAuthorizationAsync(Uri authorizationUri, Uri redirectUri, IDictionary<string, string> additionalHeaders, CallState callState)
 {
     returnedUriReady = new SemaphoreSlim(0);
     Authenticate(authorizationUri, redirectUri, additionalHeaders, callState);
     await returnedUriReady.WaitAsync().ConfigureAwait(false);
     return authorizationResult;
 }
Beispiel #16
0
		public static async Task<HttpResponseMessage> GetWithRetryAsync(this HttpClient client, string url, SemaphoreSlim semaphore, params int[] retryDelay)
		{
			if (retryDelay.Any(delay => delay <= 0))
			{
				throw new ArgumentException("Delay should be greate than 0.", nameof(retryDelay));
			}

			await semaphore.WaitAsync();
			try
			{
				int retryCount = 0;
				while (true)
				{
					try
					{
						return await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
					}
					catch (TaskCanceledException)
					{
						if (retryCount >= retryDelay.Length)
						{
							throw;
						}
					}
					await Task.Delay(retryDelay[retryCount]);
					retryCount++;
				}
			}
			finally
			{
				semaphore.Release();
			}
		}
        public async Task ProducerShouldReportCorrectAmountOfAsyncRequests()
        {
            // var log = new ConsoleLog(); for (int i = 0; i < 100; i++) {
            var semaphore = new SemaphoreSlim(0);
            var routerProxy = new FakeBrokerRouter();
            //block the second call returning from send message async
            routerProxy.BrokerConn0.ProduceResponseFunction = async () =>
            {
                await semaphore.WaitAsync();
                return new ProduceResponse();
            };

            var router = routerProxy.Create();
            using (var producer = new Producer(router, maximumAsyncRequests: 1) { BatchSize = 1 })
            {
                var messages = new[] { new Message("1") };

                Assert.That(producer.AsyncCount, Is.EqualTo(0));

                var sendTask = producer.SendMessageAsync(BrokerRouterProxy.TestTopic, messages);

                await TaskTest.WaitFor(() => producer.AsyncCount > 0);
                Assert.That(producer.AsyncCount, Is.EqualTo(1), "One async operation should be sending.");

                semaphore.Release();
                sendTask.Wait(TimeSpan.FromMilliseconds(500));
                await Task.Delay(2);
                Assert.That(sendTask.IsCompleted, Is.True, "Send task should be marked as completed.");
                Assert.That(producer.AsyncCount, Is.EqualTo(0), "Async should now show zero count.");
                // } log.DebugFormat(i.ToString());
            }
        }
Beispiel #18
0
        /// <summary>
        /// Ensures the list.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="file">The file.</param>
        /// <param name="httpClient">The HTTP client.</param>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="semaphore">The semaphore.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public static async Task EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, SemaphoreSlim semaphore, CancellationToken cancellationToken)
        {
            var fileInfo = fileSystem.GetFileInfo(file);

            if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
            {
                await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

                try
                {
                    var temp = await httpClient.GetTempFile(new HttpRequestOptions
                    {
                        CancellationToken = cancellationToken,
                        Progress = new Progress<double>(),
                        Url = url

                    }).ConfigureAwait(false);

					fileSystem.CreateDirectory(Path.GetDirectoryName(file));

					fileSystem.CopyFile(temp, file, true);
                }
                finally
                {
                    semaphore.Release();
                }
            }
        }
        public override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(this._bucketName))
            {
                throw new InvalidOperationException("The bucketName specified is null or empty!");
            }

            SemaphoreSlim asyncThrottler = null;
            CancellationTokenSource internalCts = null;
            try
            {
                asyncThrottler = new SemaphoreSlim(_config.ConcurrentServiceRequests);
                internalCts = new CancellationTokenSource();
                var internalCancellationToken = internalCts.Token;

                ListMultipartUploadsResponse listResponse = new ListMultipartUploadsResponse();
                var pendingTasks = new List<Task<AbortMultipartUploadResponse>>();
                do
                {
                    ListMultipartUploadsRequest listRequest = ConstructListMultipartUploadsRequest(listResponse);
                    listResponse = await this._s3Client.ListMultipartUploadsAsync(listRequest, cancellationToken)
                        .ConfigureAwait(continueOnCapturedContext: false);

                    foreach (MultipartUpload upload in listResponse.MultipartUploads)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        if (internalCancellationToken.IsCancellationRequested)
                        {
                            // Operation cancelled as one of the AbortMultipartUpload requests failed with an exception,
                            // don't schedule any more AbortMultipartUpload tasks. 
                            // Don't throw an OperationCanceledException here as we want to process the 
                            // responses and throw the original exception.
                            break;
                        }
                        if (upload.Initiated < this._initiatedDate)
                        {
                            await asyncThrottler.WaitAsync(cancellationToken)
                                .ConfigureAwait(continueOnCapturedContext: false);

                            var abortRequest = ConstructAbortMultipartUploadRequest(upload);
                            var task = AbortAsync(abortRequest, internalCts, cancellationToken, asyncThrottler);
                            pendingTasks.Add(task);
                        }
                    }
                }
                while (listResponse.IsTruncated);

                await WhenAllOrFirstExceptionAsync(pendingTasks,cancellationToken)
                    .ConfigureAwait(continueOnCapturedContext: false);
            }
            finally
            {
                if (internalCts != null)
                    internalCts.Dispose();

                if (asyncThrottler!=null)
                    asyncThrottler.Dispose();
            }
        }
Beispiel #20
0
		public static async Task<SemaphoreLock> TakeAsync(SemaphoreSlim semaphore)
		{
			Argument.ValidateIsNotNull(semaphore, "semaphore");

			await semaphore.WaitAsync();

			return new SemaphoreLock(semaphore);
		}
Beispiel #21
0
        public static Task <TOut[]> BatchProcess <T, TOut>(IList <T> source, Func <T, Task <TOut> > f, int max)
        {
            var initial = (max >> 1);
            var s       = new System.Threading.SemaphoreSlim(initial, max);

            _ = Task.Run(async() =>
            {
                for (int i = initial; i < max; i++)
                {
                    await Task.Delay(100);
                    s.Release();
                }
            });

            return(Task.WhenAll(from item in source
                                select Task.Run(async() =>
            {
                TOut ret = default;
                var retry = 0;
                var maxRetry = 5;
                while (retry < maxRetry)
                {
                    try
                    {
                        using (var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(60)))
                        {
                            await s.WaitAsync(cancellationTokenSource.Token);
                            try
                            {
                                ret = await f(item);
                                break;
                            }
                            catch (System.OperationCanceledException e)
                            {
                                Log.Warning($"see cancellation in {f.Method.Name}: {e.Message}");
                            }
                            finally
                            {
                                s.Release();
                            }
                        }
                    }
                    catch (System.OperationCanceledException)
                    {
                        Log.Warning($"Waiting too long time to obtain the semaphore: current: {s.CurrentCount}, max: {max}");
                    }
                    var rand = new Random();
                    var randomDelay = TimeSpan.FromMilliseconds(rand.Next(1, 500));
                    await Task.Delay(randomDelay);
                    retry++;
                }
                if (retry == maxRetry)
                {
                    Log.Error($"The operation {f.Method.Name} was canceled because of reaching max retry {maxRetry}");
                }
                return ret;
            })));
        }
Beispiel #22
0
        public async Task <EventBody> WaitForInputMessageAsync(string connectionId, string inputName)
        {
            Console.WriteLine("WaitForInputMessageAsync received for {0} with inputName {1}", connectionId, inputName);
            var mutex = new System.Threading.SemaphoreSlim(1);
            await mutex.WaitAsync().ConfigureAwait(false);  // Grab the mutex. The handler will release it later

            byte[]         bytes   = null;
            var            client  = objectMap[connectionId];
            MessageHandler handler = async(msg, context) =>
            {
                Console.WriteLine("message received");
                bytes = msg.GetBytes();
                await client.SetInputMessageHandlerAsync(inputName, null, null).ConfigureAwait(false);

                Console.WriteLine("releasing inputMessage mutex");
                mutex.Release();
                return(MessageResponse.Completed);
            };

            Console.WriteLine("Setting input handler");
            await client.SetInputMessageHandlerAsync(inputName, handler, null).ConfigureAwait(false);

            Console.WriteLine("Waiting for inputMessage mutex to release");
            await mutex.WaitAsync().ConfigureAwait(false);

            Console.WriteLine("mutex triggered.");

            string s = Encoding.UTF8.GetString(bytes);

            Console.WriteLine("message = {0}", s as object);
            object result;

            try
            {
                result = JsonConvert.DeserializeObject(s);
            }
            catch (JsonReaderException)
            {
                result = s;
            }
            return(new Models.EventBody
            {
                Body = result
            });
        }
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="SemaphoreSlimRequest"/> class.
        /// </summary>
        /// <param name="semaphore">The semaphore.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public SemaphoreSlimRequest(SemaphoreSlim semaphore, CancellationToken cancellationToken)
        {

            _semaphore = Ensure.IsNotNull(semaphore, nameof(semaphore));

            _disposeCancellationTokenSource = new CancellationTokenSource();
            _linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _disposeCancellationTokenSource.Token);
            _task = semaphore.WaitAsync(_linkedCancellationTokenSource.Token);
        }
Beispiel #24
0
        /// <summary>
        /// Blocks the current thread until the current object receives a signal.
        /// </summary>
        /// <param name="millisecondsTimeout">The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.</param>
        /// /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>Returns a task</returns>
        public async Task <bool> WaitAsync(int millisecondsTimeout = 0, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (await _currentObject.WaitAsync(millisecondsTimeout, cancellationToken))
            {
                IsLockOwnedFlag = true;
            }

            return(IsLockOwnedFlag);
        }
Beispiel #25
0
		public static async Task<SemaphoreLock> TryTakeAsync(SemaphoreSlim semaphore, TimeSpan timeout)
		{
			Argument.ValidateIsNotNull(semaphore, "semaphore");

			if (await semaphore.WaitAsync(timeout))
				return new SemaphoreLock(semaphore);
			else
				return null;
		}
 private async Task CancellationTask(SemaphoreSlim cancellationSemaphore, CancellationToken cancellationToken)
 {
     try
     {
         await cancellationSemaphore.WaitAsync(cancellationToken);
     }
     catch (OperationCanceledException)
     {
     }
 }
Beispiel #27
0
        public async Task LoadUsers(
            LoadOptions options ) {

            long trafficUsed = 0, usersLoaded = 0;
            var p = options.UseCounter ? (IIdProvdier) new RangeIdProvider( options.Start, options.End ) : new FileIdProvdier( options.SourceFile );
            int activeThreads = 0;
            using ( var semaphore = new SemaphoreSlim( options.Threads ) ) {

                Func<int[], Task> getChunk = async i => {
                    try {
                        int[] users = i;
                        if ( options.CancellationToken?.Invoke() ?? false ) return;
                        var outfile = GetChunkPath(options, users );
                        if ( !File.Exists( outfile ) ) {
                            var resp = await GetUsers( options, users ).ConfigureAwait(false);
                            if ( resp == null ) return;
                            await SaveFile( outfile, resp, options.GZip ).ConfigureAwait(false);
                            usersLoaded += users.Length;
                            trafficUsed += _textEncoding.GetByteCount( resp );
                            options.ShowTraffic?.Invoke( trafficUsed );
                            options.ShowCount?.Invoke( usersLoaded );
                        }
                    }
                    catch {}
                    finally {
                        --activeThreads;
                        semaphore.Release();
                    }
                };

                int[] current;
                while ( (current=await p.Get( options.VolumeSize ).ConfigureAwait( false )).Any() ) {
                    if (options.CancellationToken?.Invoke() ?? false ) break;
                    await semaphore.WaitAsync().ConfigureAwait(false);
                    Console.WriteLine( "Threads: {0}", ++activeThreads );
                    var tsk = getChunk( current );
                    if ( options.DelayEnabled)
                        await Task.Delay( options.Delay ).ConfigureAwait(false);
                }
                for ( int i = 0; i < options.Threads; i++ ) await semaphore.WaitAsync().ConfigureAwait( false );
            }
        }
Beispiel #28
0
        public async Task Invoke(IncomingContext context)
        {
            int currentIndex = 0;
            var semaphore = new SemaphoreSlim(1);
            Stack<Tuple<Task, TaskCompletionSource<ExceptionDispatchInfo>>> sources = new Stack<Tuple<Task, TaskCompletionSource<ExceptionDispatchInfo>>>();

            while (currentIndex < executingElements.Count)
            {
                await semaphore.WaitAsync().ConfigureAwait(false);

                var element = executingElements[currentIndex];
                currentIndex += 1;

                var tcs = new TaskCompletionSource<ExceptionDispatchInfo>();
                var task = element.Invoke(context, () =>
                {
                    semaphore.Release();
                    return tcs.Task;
                });

                sources.Push(Tuple.Create(task, tcs));
            }

            ExceptionDispatchInfo exception = null;
            var anotherSemaphore = new SemaphoreSlim(1);
            var allTasks = new ConcurrentBag<Task>();
            foreach (var source in sources)
            {
                await anotherSemaphore.WaitAsync().ConfigureAwait(false);

                if (exception != null)
                {
                    context.Exceptions.Enqueue(exception);
                    source.Item2.TrySetException(exception.SourceException);
                    exception = null;
                }
                else
                {
                    source.Item2.TrySetResult(null);
                }

                var task = source.Item1.ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        exception = ExceptionDispatchInfo.Capture(t.Exception.InnerException);
                    }
                    anotherSemaphore.Release();
                }, TaskContinuationOptions.ExecuteSynchronously);
                allTasks.Add(task);
            }

            await Task.WhenAll(allTasks).ConfigureAwait(false);
        }
        public IList<WorkItemModel> GetWorkItemCollection()
        {
            IEnumerable<ICloudWorkItem> workItems = this.Service.ListWorkItems(OptionsModel.Instance.ListDetailLevel);

            using (var sem = new SemaphoreSlim(MaxJobRequestsInFlight))
            {
                var workItemsTasks = workItems.Select(
                    async cloudWorkItem =>
                        {
                            if (cloudWorkItem.ExecutionInformation != null && cloudWorkItem.ExecutionInformation.RecentJob != null)
                            {
                                try
                                {
                                    await sem.WaitAsync();
                                    try
                                    {
                                        var latestJob = await cloudWorkItem.GetJobAsync(cloudWorkItem.ExecutionInformation.RecentJob.Name);
                                        return new WorkItemModel(cloudWorkItem, latestJob);
                                    }
                                    finally
                                    {
                                        sem.Release();
                                    }
                                }
                                catch (BatchException be)
                                {
                                    if (be.RequestInformation != null && be.RequestInformation.AzureError != null && be.RequestInformation.AzureError.Code == BatchErrorCodeStrings.JobNotFound)
                                    {
                                        return new WorkItemModel(cloudWorkItem);
                                    }

                                    return null;
                                }
                                catch (Exception)
                                {
                                    // eat the exception for now
                                    return null;
                                }
                            }
                            else
                            {
                                return new WorkItemModel(cloudWorkItem);
                            }
                        })
                    .ToArray();

                Task.WaitAll(workItemsTasks);

                return workItemsTasks
                    .Select(task => task.Result)
                    .Where(workItemModel => workItemModel != null)
                    .ToList();
            }
        }
Beispiel #30
0
 private static async Task InSemaphore(SemaphoreSlim sync, Func<Task> func)
 {
     await sync.WaitAsync();
     try
     {
         await func();
     }
     finally
     {
         sync.Release();
     }
 }
Beispiel #31
0
        public async Task <object> WaitForDesiredPropertyPatchAsync(string connectionId)
        {
            // Since there's no way to un-register for a patch, we have a global patch handler.  We keep the
            // "last desired props received" in a member varaible along with a mutex to trigger when this changes.
            // Not very cool and not very thread safe :(
            Console.WriteLine("WaitForDesiredPropertyPatchAsync received for " + connectionId);
            var client = objectMap[connectionId];
            var mutex  = new System.Threading.SemaphoreSlim(1);
            await mutex.WaitAsync().ConfigureAwait(false);  // Grab the mutex. The handler will release it later

            desiredPropMutex = mutex;

            Console.WriteLine("Waiting for patch");
            await mutex.WaitAsync().ConfigureAwait(false);

            Console.WriteLine("mutex triggered.");

            Console.WriteLine("Returning patch:");
            Console.WriteLine(JsonConvert.SerializeObject(lastDesiredProps));
            return(lastDesiredProps);
        }
        public async Task ExecuteAsync(ITestPattern testPattern, IWorkerStrategy execStrategy, CancellationToken cancelToken)
        {
            SemaphoreSlim cancelSignaled = new SemaphoreSlim(0, 1);

            using (cancelToken.Register(() => cancelSignaled.Release()))
            {
                for (int i = 0; i < this.WorkerCount; i++)
                {
                    execStrategy.SpawnWorker(testPattern, cancelToken);
                }

                await cancelSignaled.WaitAsync();
            }
        }
 private async Task WaitAsync(SemaphoreSlim s, CancellationTokenSource cts)
 {
     try
     {
         await s.WaitAsync();
         await Task.Delay(10);
         cts.Cancel();
     }
     catch (OperationCanceledException) { }
     finally
     {
         s.Release();
     }
 }
Beispiel #34
0
        static async Task Run()
        {
            var clinet = new HttpClient();

            var semaphore = new SemaphoreSlim(Concurrency);
            var tasks = new List<Task>();
            for (int i = 0; i < RequestCount; i++)
            {
                await semaphore.WaitAsync();
                var task = clinet.GetStringAsync(EndPoint)
                    .ContinueWith((_, state) => ((SemaphoreSlim)state).Release(), semaphore);
                tasks.Add(task);
            }

            await Task.WhenAll(tasks);
        }
Beispiel #35
0
        /// <inheritdoc />
        public async Task <IHttpContext> GetContextAsync(CancellationToken ct)
        {
            while (true)
            {
                await _ctxQueueSem.WaitAsync(ct).ConfigureAwait(false);

                foreach (var key in _ctxQueue.Keys)
                {
                    if (_ctxQueue.TryRemove(key, out var context))
                    {
                        return(context);
                    }

                    break;
                }
            }
        }
        private async Task WaitWithCancellationTokenAsync(SemaphoreSlim s, CancellationToken ct)
        {
            try
            {
                await s.WaitAsync(ct);

                if (ct.IsCancellationRequested)
                    return;

                await Task.Delay(10);
            }
            catch (OperationCanceledException) { }
            finally
            {
                s.Release();
            }
        }
Beispiel #37
0
        private async Task ThrottlerMethod(CancellationToken cancellationToken)
        {
            using (var throttler = new SemaphoreSlim(initialCount: this.maxWorkerCount, maxCount: this.maxWorkerCount))
                while (!cancellationToken.IsCancellationRequested)
                {
                    await throttler.WaitAsync();
                    var proxy = await this.proxyBag.ReceiveAsync(cancellationToken);
 
                    await this.isRunningTask;
 
                    Task.Run(async () =>
                    {
                        try { await WorkerMethod(proxy); }
                        finally { throttler.Release(); }
                    });
                }
        }
Beispiel #38
0
    public static async Task SerializeAsync <T>(T content, Windows.Storage.StorageFolder folder, string fileName, System.Threading.SemaphoreSlim semaphore)
    {
        await semaphore.WaitAsync();

        try
        {
            var f = await folder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting);

            using var s = (await f.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)).AsStream();
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            serializer.Serialize(s, content);
        }
        catch { }
        finally
        {
            semaphore.Release();
        }
    }
Beispiel #39
0
        private async void VideoDevice_FrameArrived(object sender, NewFrameEventArgs eventArgs)
        {
            if (SettingService.IsMirror)
            {
                eventArgs.Frame.RotateFlip(RotateFlipType.RotateNoneFlipX);
            }

            if (!await semaphore.WaitAsync(0))
            {
                return;
            }

            try
            {
                using (Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone())
                {
                    var detectedBarcode = await barcodeService.DetectBarcodeFromBitmapAsync(bitmap);

                    if (detectedBarcode != null)
                    {
                        if (lastDetectedBarcodeValue != detectedBarcode.BarcodeValue)
                        {
                            WriteTextSafe(detectedBarcode.BarcodeValue);

                            if (SettingService.IsSaveToCSV)
                            {
                                ExportCSV(detectedBarcode);
                            }

                            if (SettingService.IsCopyToClipboard)
                            {
                                CopyToClipboard(detectedBarcode);
                            }

                            lastDetectedBarcodeValue = detectedBarcode.BarcodeValue;
                        }
                    }
                }
            }
            finally
            {
                semaphore.Release();
            }
        }
        public static async Task<IList<IDictionary<AFSummaryTypes, AFValue>>> GetSummariesAsyncThrottled(AFAttributeList attributeList, int numConcurrent)
        {
            // Use "asynchronous semaphore" pattern (e.g. SemaphoreSlim.WaitAsync()) to throttle the calls

            Console.WriteLine("Calling GetSummariesAsyncThrottled");

            // Example: Limit to numConcurrent concurrent async I/O operations.
            SemaphoreSlim throttler = new SemaphoreSlim(initialCount: numConcurrent);

            Task<IDictionary<AFSummaryTypes, AFValue>>[] tasks = attributeList
                // Do not make the call if async is not supported
                .Where(attr => (attr.SupportedDataMethods & AFDataMethods.Asynchronous) == AFDataMethods.Asynchronous)
                .Select(async attr =>
                {
                    // asychronously try to acquire the semaphore
                    await throttler.WaitAsync();

                    try
                    {
                        AFSummaryTypes mySummaries = AFSummaryTypes.Minimum | AFSummaryTypes.Maximum | AFSummaryTypes.Average | AFSummaryTypes.Total;
                        AFTimeRange timeRange = new AFTimeRange(new AFTime("*-1d"), new AFTime("*"));

                        return await attr.Data.SummaryAsync(
                            timeRange: timeRange,
                            summaryType: mySummaries,
                            calculationBasis: AFCalculationBasis.TimeWeighted,
                            timeType: AFTimestampCalculation.Auto);
                    }
                    catch (AggregateException ae)
                    {
                        Console.WriteLine("{0}: {1}", attr.Name, ae.Flatten().InnerException.Message);
                        return null;
                    }
                    finally
                    {
                        // release the resource
                        throttler.Release();
                    }
                })
                .ToArray();

            return await Task.WhenAll(tasks);
        }
Beispiel #41
0
        public Task StartAsync(Func<TransportMessage, Task> onMessage)
        {
            onMessageAsync = onMessage;

            runningTasks = new ConcurrentDictionary<Task, Task>();
            semaphore = new SemaphoreSlim(maxConcurrency);
            tokenSource = new CancellationTokenSource();
            var token = tokenSource.Token;

            pumpTask = Task.Factory.StartNew(async () =>
            {
                while (!token.IsCancellationRequested)
                {
                    await semaphore.WaitAsync(token);

                    TransportMessage message;
                    if (messages.TryDequeue(out message))
                    {
                        var task = onMessageAsync(message);

                        runningTasks.AddOrUpdate(task, task, (k, v) => task)
                            .Ignore();

                        task.ContinueWith(t =>
                        {
                            semaphore.Release();
                            Task taskToBeRemoved;
                            runningTasks.TryRemove(t, out taskToBeRemoved);
                        }, TaskContinuationOptions.ExecuteSynchronously)
                            .Ignore();
                    }
                    else
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(20), token).ConfigureAwait(false);
                    }

                }
            }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default)
            .Unwrap();

            return Task.CompletedTask;
        }
        public async Task Executor_should_stop_monitoring_item_if_its_task_has_finished_so_it_should_be_possible_to_register_it_again()
        {
            var taskName = "abc";

            var taskFinished = new SemaphoreSlim(0);

            using (var executor = ContinuousTaskExecutor<string>.StartExecutor(Mock.Of<ITimeCoordinator>()))
            {
                executor.FinishedTaskFor += item =>
                {
                    if (item == taskName) taskFinished.Release();
                };

                Assert.True(executor.TryRegisterTaskFor(taskName, (item, token) => Task.Delay(25, token)));

                await taskFinished.WaitAsync(_testTimeout);

                Assert.True(executor.TryRegisterTaskFor(taskName, (item, token) => Task.FromResult(0)), "It should be possible to register task again");
            }
        }
Beispiel #43
0
        /// <summary>
        /// Creates grouping subscriptions and waits for the notification events to flow
        /// </summary>
        /// <param name="impersonationId"></param>
        /// <returns></returns>
        async public System.Threading.Tasks.Task CreateStreamingSubscriptionGroupingAsync(string impersonationId)
        {
            var database = EWSConstants.Config.Database;

            using (EWSDbContext context = new EWSDbContext(database))
            {
                var smtpAddresses = await context.RoomListRoomEntities.ToListAsync();

                foreach (var sMailbox in smtpAddresses)
                {
                    var addedBox = _mailboxes.AddMailbox(sMailbox.SmtpAddress);
                    if (!addedBox)
                    {
                        _traceListener.Trace("SyncProgram", $"Failed to add SMTP {sMailbox.SmtpAddress}");
                    }

                    MailboxInfo mailboxInfo = _mailboxes.Mailbox(sMailbox.SmtpAddress);
                    if (mailboxInfo != null)
                    {
                        GroupInfo groupInfo = null;
                        if (_groups.ContainsKey(mailboxInfo.GroupName))
                        {
                            groupInfo = _groups[mailboxInfo.GroupName];
                        }
                        else
                        {
                            groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, EwsToken, _traceListener);
                            _groups.Add(mailboxInfo.GroupName, groupInfo);
                        }

                        if (groupInfo.Mailboxes.Count > 199)
                        {
                            // We already have enough mailboxes in this group, so we rename it and create a new one
                            // Renaming it means that we can still put new mailboxes into the correct group based on GroupingInformation
                            int i = 1;
                            while (_groups.ContainsKey($"{groupInfo.Name}-{i}"))
                            {
                                i++;
                            }

                            // Remove previous grouping name from stack
                            _groups.Remove(groupInfo.Name);

                            // Add the grouping back with the new grouping name [keep the GroupInfo with previous name]
                            _groups.Add($"{groupInfo.Name}-{i}", groupInfo);

                            // Provision a new GroupInfo with the GroupName
                            groupInfo = new GroupInfo(mailboxInfo.GroupName, mailboxInfo.SMTPAddress, mailboxInfo.EwsUrl, EwsToken, _traceListener);

                            // Add GroupInfo to stack
                            _groups.Add(mailboxInfo.GroupName, groupInfo);
                        }

                        // Add the mailbox to the collection
                        groupInfo.Mailboxes.Add(sMailbox.SmtpAddress);
                    }
                }

                // Enable the Grouping
                foreach (var _group in _groups)
                {
                    _traceListener.Trace("SyncProgram", $"Opening connections for {_group.Key}");

                    var groupName = _group.Key;
                    var groupInfo = _group.Value;

                    // Create a streaming connection to the service object, over which events are returned to the client.
                    // Keep the streaming connection open for 30 minutes.
                    if (AddGroupSubscriptions(context, groupName))
                    {
                        _traceListener.Trace("SyncProgram", $"{groupInfo.Mailboxes.Count()} mailboxes primed for StreamingSubscriptions.");
                    }
                    else
                    {
                        _traceListener.Trace("SyncProgram", $"Group {groupInfo.Name} failed in StreamingSubscription events.");
                    }
                }
            }

            using (var semaphore = new System.Threading.SemaphoreSlim(1))
            {
                // Block the Thread
                semaphore.Wait();

                // Establish the StreamingSubscriptionConnections based on the GroupingInfo
                foreach (var connection in _connections)
                {
                    var _group      = _groups[connection.Key];
                    var _connection = connection.Value;
                    if (_connection.IsOpen)
                    {
                        _group.IsConnectionOpen = true;
                        return;
                    }

                    try
                    {
                        _connection.Open();
                        _group.IsConnectionOpen = true;
                    }
                    catch (Exception ex)
                    {
                        _traceListener.Trace("SyncProgram", $"Error opening streamingsubscriptionconnection for group {_group.Name} MSG {ex.Message}");
                    }
                }

                // Lock the Thread Until its cancelled or fails
                await semaphore.WaitAsync(CancellationTokenSource.Token);
            }
        }
Beispiel #44
0
        public static async Task <IDisposable> Lock(this System.Threading.SemaphoreSlim semaphore)
        {
            await semaphore.WaitAsync();

            return(new LockRelease(semaphore));
        }
Beispiel #45
0
        async Task <string> CutByCode(string dataInput)//the method: double(double C,double E) //count, entropy, return score
        {
            var counter = System.Threading.Interlocked.Increment(ref counter_CutByCode);

            try
            {
                await SemaphoreSlim_CutByCode.WaitAsync();

                if (counter != System.Threading.Interlocked.Read(ref counter_CutByCode))
                {
                    return(null);
                }
                const string namespaceName = "WikiDataAnalysis", className = "FooClass", methodName = "FooMethod";
                string       code =
                    "using System;" +
                    $"namespace {namespaceName}" +
                    "{" +
                    $"   class {className}" +
                    "   {" +
                    $"       public static double {methodName}(string S,int N,Func<string,int> C)" +
                    "       {" +
                    $"           {dataInput}" +
                    "       }" +
                    "   }" +
                    "}";
                System.Reflection.MethodInfo methodInfo;
                try
                {
                    Trace.Indent();
                    Trace.WriteLine($"Compiling... code length = {code.Length}");
                    methodInfo = Utils.DynamicCompile.GetMethod(code, namespaceName, className, methodName, "System");
                    var method = new Func <string, int, Func <string, int>, double>((s, n, c) => (double)methodInfo.Invoke(null, new object[] { s, n, c }));
                    Trace.WriteLine("Splitting...");
                    var           maxWordLength = int.Parse(IFdata.GetField("maxWordLength"));
                    var           probRatio     = double.Parse(IFdata.GetField("probRatio"));
                    var           bemsRatio     = double.Parse(IFdata.GetField("bemsRatio"));
                    StringBuilder sb_ret        = new StringBuilder();
                    long          cnt           = 0;
                    await Task.Run(() =>
                    {
                        var mainInputs = string.IsNullOrWhiteSpace(TXBdata.Text) ? (txbDataFileContent != null ? txbDataFileContent : data) : TXBdata.Text;
                        var inputs     = mainInputs.Split(' ', '\r', '\n', '\t');
                        if (ss_CutByCode == null)
                        {
                            ss_CutByCode = new SentenceSplitter(sa);
                        }
                        const int maxoutputLength = 10000;
                        bool appending            = true;
                        int progress       = 0, total_progress = inputs.Length;
                        var lastUpdateTime = DateTime.MinValue;
                        foreach (var input in inputs)
                        {
                            ++progress;
                            if ((DateTime.Now - lastUpdateTime).TotalSeconds > 0.5)
                            {
                                Trace.WriteLine($"Splitting... {progress}/{total_progress}");
                                lastUpdateTime = DateTime.Now;
                            }
                            var cutResult = ss_CutByCode.Split(input, maxWordLength, method, false);
                            cnt          += cutResult.Count;
                            if (sb_ret.Length + cutResult.Sum(s => (long)s.Length) > maxoutputLength)
                            {
                                appending = false;
                            }
                            if (appending)
                            {
                                sb_ret.AppendLine(string.Join(" ", cutResult));
                            }
                        }
                    });

                    Trace.WriteLine($"{cnt} words identified.");
                    return(sb_ret.ToString());
                }
                catch (Exception error) { return(error.ToString()); }
                finally { Trace.Unindent(); }
            }
            finally { lock (SemaphoreSlim_CutByCode) SemaphoreSlim_CutByCode.Release(); }
        }
Beispiel #46
0
        protected async ValueTask InitilizeAsync()
        {
            if (initilized)
            {
                await TestSemaphoreSlim.WaitAsync();
                await RunBrowserAsync();

                Page = await Browser.NewPageAsync();

                await Page.GoToAsync("https://localhost:5001");

                return;
            }
            await InitilizeSemaphoreSlim.WaitAsync();

            try
            {
                if (initilized)
                {
                    await TestSemaphoreSlim.WaitAsync();
                    await RunBrowserAsync();

                    Page = await Browser.NewPageAsync();

                    await Page.GoToAsync("https://localhost:5001");

                    return;
                }
                Output.WriteLine("启动服务器");
                hostBuilder = Program.CreateHostBuilder(new string[0]);
                source      = new System.Threading.CancellationTokenSource();
                var host = hostBuilder.Build();
                _ = host.RunAsync(source.Token);
                demoTesterTypes = AppDomain.CurrentDomain.GetAssemblies()
                                  .Where(x => !x.IsDynamic)
                                  .SelectMany(x => x.ExportedTypes)
                                  .Where(x => x.GetInterface(nameof(IDemoTester)) != null)
                                  .Select(x =>
                {
                    var testNameAttribute = x.GetCustomAttributes(false).OfType <TestNameAttribute>().FirstOrDefault();
                    return(new
                    {
                        Menu = testNameAttribute.MenuName,
                        testNameAttribute.Name,
                        Type = x
                    });
                })
                                  .GroupBy(x => x.Menu)
                                  .ToDictionary(x => x.Key, x => x.ToDictionary(y => y.Name, y => y.Type));
                Output.WriteLine("下载浏览器");
                var fetcher = new BrowserFetcher();
                if (!File.Exists(fetcher.DownloadsFolder))
                {
                    await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision);
                }
                else
                {
                    await Task.Delay(1000);
                }
                await TestSemaphoreSlim.WaitAsync();
                await RunBrowserAsync();

                Page = await Browser.NewPageAsync();

                await Page.GoToAsync("https://localhost:5001");

                Output.WriteLine("初始化完成");
                initilized = true;
            }
            finally
            {
                InitilizeSemaphoreSlim.Release();
            }
        }