System.Threading.SemaphoreSlim.WaitAsync()

Here are the examples of the csharp api System.Threading.SemaphoreSlim.WaitAsync() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1283 Examples 7

19 View Source File : AsyncAwaitor.cs
License : MIT License
Project Creator : angelsix

public static async Task<T> AwaitResultAsync<T>(string key, Func<Task<T>> task, int maxAccessCount = 1)
        {
            #region Create Semapreplaced

            //
            // Asynchronously wait to enter the Semapreplaced
            //
            //      If no-one has been granted access to the Semapreplaced
            //      code execution will proceed
            //      Otherwise this thread waits here until the semapreplaced is released 
            //
            await SelfLock.WaitAsync();

            try
            {
                // Create semapreplaced if it doesn't already exist
                if (!Semapreplaceds.ContainsKey(key))
                    Semapreplaceds.Add(key, new SemapreplacedDetails(key, maxAccessCount));
            }
            finally
            {
                //
                // When the task is ready, release the semapreplaced
                //
                //      It is vital to ALWAYS release the semapreplaced when we are ready
                //      or else we will end up with a Semapreplaced that is forever locked
                //      This is why it is important to do the Release within a try...finally clause
                //      Program execution may crash or take a different path, this way you are guaranteed execution
                //
                SelfLock.Release();
            }

            #endregion

            // Now use this semapreplaced and perform the desired task inside its lock
            // NOTE: We never remove semapreplaceds after creating them, so this will never be null
            var semapreplaced = Semapreplaceds[key];

            // Await this semapreplaced
            await semapreplaced.Semapreplaced.WaitAsync();

            try
            {
                // Perform the job
                return await task();
            }
            finally
            {
                // Release the semapreplaced
                semapreplaced.Semapreplaced.Release();
            }
        }

19 View Source File : DownloaderViewModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

internal void CloseApp(object obj)
        {
            var window = (Window)obj;

            if (_ctsUpdatingList != null)
            {
                if (_displayMessage.Invoke(
                    "Background operation in progress. Cancel and exit program?", "Exit",
                    MessageBoxButton.YesNo, MessageBoxImage.Exclamation, MessageBoxResult.No) == MessageBoxResult.No)
                {
                    return;
                }
                window.IsEnabled = false;
                window.replacedle = "Quitting, please wait...";
                _ctsUpdatingList.Cancel();
            }
            this.Status = "Saving data...";
            RaisePropertyChanged(nameof(this.Status));
            if (QueueProcessor.IsBusy) QueueProcessor.Stop();
            Task t = Task.Run(async () =>
            {
                await _semapreplacedUpdatingList.WaitAsync();
                try
                {
                    Directory.CreateDirectory(AppPaths.LocalAppData);
                    var writer = new XmlSerializer(typeof(SerializableDownloaderObjectModelList));
                    var list = new SerializableDownloaderObjectModelList();
                    var index = 0;
                    foreach (var item in DownloadItemsList)
                    {
                        if (item.IsBeingDownloaded) item.Pause();
                        if (item.Status == DownloadStatus.Finished && Settings.Default.ClearFinishedOnExit) continue;
                        var sItem = new SerializableDownloaderObjectModel
                        {
                            Index = index++,
                            Url = item.Url,
                            Destination = item.Destination,
                            TotalBytesToDownload = item.TotalBytesToDownload,
                            IsQueued = item.IsQueued,
                            IsCompleted = item.IsCompleted,
                            DateCreated = item.DateCreated,
                            StatusCode = item.StatusCode
                        };
                        list.Objects.Add(sItem);
                    }
                    using (var streamWriter = new StreamWriter(AppPaths.DownloadsHistoryFile, false))
                    {
                        writer.Serialize(streamWriter, list);
                    }
                }
                catch
                {
                    return;
                }
                finally
                {
                    _semapreplacedUpdatingList.Release();
                }
            }).ContinueWith(t =>
            {
                Settings.Default.Save();
                try
                {
                    Application.Current?.Dispatcher.Invoke(Application.Current.Shutdown);
                }
                catch
                {
                }
            });
        }

19 View Source File : Localization.cs
License : MIT License
Project Creator : AkiniKites

public async Task<string> GetString(string file, BaseGGUUID id)
        {
            if (!_cache.TryGetValue(file, out var texts))
            {
                await _lock.WaitAsync();
                try
                {
                    if (!_cache.TryGetValue(file, out texts))
                    {
                        texts = await LoadFile(file);
                        _cache[file] = texts;
                    }
                }
                finally
                {
                    _lock.Release();
                }
            }

            if (texts.TryGetValue(id, out var val))
                return val;
            return null;
        }

19 View Source File : SasTokenProvider.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

public async Task<string> GetSasToken(string org)
        {
            (DateTime created, string token) sasToken;
            if (_sasTokens.TryGetValue(org, out sasToken) && StillYoung(sasToken.created))
            {
                return sasToken.token;
            }

            _sasTokens.TryRemove(org, out _);

            await _semapreplaced.WaitAsync();
            try
            {
                if (_sasTokens.TryGetValue(org, out sasToken))
                {
                    return sasToken.token;
                }

                string storageAccount = string.Format(_storageAccount, org, _environment);
                string sasDefinition = string.Format(_sasDefinition, org, _environment);
                string secretName = $"{storageAccount}-{sasDefinition}";
                string keyVaultUri = string.Format(_keyVaultURI, org, _environment);

                sasToken.token = await _keyVaultService.GetSecretAsync(keyVaultUri, secretName);
                sasToken.created = DateTime.UtcNow;

                _sasTokens.TryAdd(org, sasToken);

                return sasToken.token;
            }
            catch (Exception e)
            {
                _logger.LogError($"SasTokenProvider // GetSasToken // Exeption: {e}");
                return string.Empty;
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

19 View Source File : GroupContentsControlViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private async Task LoadMoreAsync(bool updateNewest)
        {
            await this.ReloadSem.WaitAsync();

            try
            {
                ICollection<Message> results;

                if (this.FirstDisplayedMessage == null || updateNewest)
                {
                    // load the most recent messages
                    results = await this.MessageContainer.GetMessagesAsync();

                    this.CacheManager.SuperIndexer.SubmitGroupUpdate(this.MessageContainer, results);
                }
                else
                {
                    // load the 20 (GroupMe default) messages preceeding the oldest (first) one displayed
                    results = await this.MessageContainer.GetMessagesAsync(GroupMeClientApi.MessageRetreiveMode.BeforeId, this.FirstDisplayedMessage.Id);
                }

                await this.UpdateDisplay(results);

                // if everything was successful, reset the reliability monitor
                this.ReliabilityStateMachine.Succeeded();
                this.RetryTimer?.Dispose();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Exception in {nameof(this.LoadMoreAsync)} - {ex.Message}. Retrying...");
                this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(async () => await this.LoadMoreAsync(updateNewest));
            }
            finally
            {
                this.ReloadSem.Release();
            }
        }

19 View Source File : SiteRepo.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task<Site> CreateSite(string newSiteName, bool openToUpload, bool openToDownload, string appid)
        {
            await _createSiteLock.WaitAsync();
            try
            {
                await _dbContext.Sites.EnsureUniqueString(t => t.SiteName, newSiteName);
                var newRootFolder = new Folder
                {
                    FolderName = "blob"
                };
                await _dbContext.Folders.AddAsync(newRootFolder);
                await _dbContext.SaveChangesAsync();
                var site = new Site
                {
                    AppId = appid,
                    SiteName = newSiteName.ToLower(),
                    RootFolderId = newRootFolder.Id,
                    OpenToUpload = openToUpload,
                    OpenToDownload = openToDownload
                };
                await _dbContext.Sites.AddAsync(site);
                await _dbContext.SaveChangesAsync();
                return site;
            }
            finally
            {
                _createSiteLock.Release();
            }
        }

19 View Source File : ServiceBusMessageQueue.cs
License : MIT License
Project Creator : ansel86castro

private async Task SubscribeInternal(BindingInfo binding)
        {
            await _semapreplacedSlim.WaitAsync();

            try
            {
                if (_options.Subscription.Name == null)
                {
                    _options.Subscription.Name = Guid.NewGuid().ToString();
                }

                var subscriptionName = _options.Subscription.Name;

                if (!_receivers.TryGetValue(binding.Exchange, out var processor))
                {
                    if (_options.Subscription.Create && ! await _adminClient.SubscriptionExistsAsync(binding.Exchange, subscriptionName))
                    {
                        await CreateTopicForSubscriptionAsync(binding.Exchange);
                        try
                        {
                            await _adminClient.CreateSubscriptionAsync(new CreateSubscriptionOptions(binding.Exchange, subscriptionName)
                            {
                                MaxDeliveryCount = _options.Subscription.MaxDeliveryCount,
                            });
                        }
                        catch (ServiceBusException e) when (e.Reason == ServiceBusFailureReason.MessagingEnreplacedyAlreadyExists)
                        { }
                    }

                    processor = _sbClient.CreateProcessor(binding.Exchange, subscriptionName, new ServiceBusProcessorOptions
                    {
                        MaxConcurrentCalls = _options.Subscription.MaxConcurrentCalls,
                        MaxAutoLockRenewalDuration = TimeSpan.FromMinutes(_options.Subscription.MaxAutoLockRenewalDurationMinutes),
                        AutoCompleteMessages = _options.Subscription.AutoComplete
                    });

                    _receivers.Add(binding.Exchange, processor);

                    processor.ProcessMessageAsync += MessageHandler;
                    processor.ProcessErrorAsync += ErrorHandler;
                    await processor.StartProcessingAsync();
                }

                try
                {
                    await _adminClient.CreateRuleAsync(binding.Exchange, subscriptionName, new CreateRuleOptions
                    {
                        Filter = new CorrelationRuleFilter(binding.Topic),
                        Name = $"{binding.Exchange}_{ GetSubscriptionName(binding.Topic)}"
                    });
                }
                catch (ServiceBusException e) when (e.Reason == ServiceBusFailureReason.MessagingEnreplacedyAlreadyExists)
                { }
            }
            finally
            {
                _semapreplacedSlim.Release();
            }
        }

19 View Source File : WebSocketClient.cs
License : MIT License
Project Creator : Aiko-IT-Systems

public async Task ConnectAsync(Uri uri)
        {
            // Disconnect first
            try { await this.DisconnectAsync().ConfigureAwait(false); } catch { }

            // Disallow sending messages
            await this._senderLock.WaitAsync().ConfigureAwait(false);

            try
            {
                // This can be null at this point
                this._receiverTokenSource?.Dispose();
                this._socketTokenSource?.Dispose();

                this._ws?.Dispose();
                this._ws = new ClientWebSocket();
                this._ws.Options.Proxy = this.Proxy;
                this._ws.Options.KeepAliveInterval = TimeSpan.Zero;
                if (this._defaultHeaders != null)
                    foreach (var (k, v) in this._defaultHeaders)
                        this._ws.Options.SetRequestHeader(k, v);

                this._receiverTokenSource = new CancellationTokenSource();
                this._receiverToken = this._receiverTokenSource.Token;

                this._socketTokenSource = new CancellationTokenSource();
                this._socketToken = this._socketTokenSource.Token;

                this._isClientClose = false;
                this._isDisposed = false;
                await this._ws.ConnectAsync(uri, this._socketToken).ConfigureAwait(false);
                this._receiverTask = Task.Run(this.ReceiverLoopAsync, this._receiverToken);
            }
            finally
            {
                this._senderLock.Release();
            }
        }

19 View Source File : DownloaderViewModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

private void StartReportingSpeed()
        {
            if (_semapreplacedMeasuringSpeed.CurrentCount == 0) return;

            Task.Run(async () =>
            {
                await _semapreplacedMeasuringSpeed.WaitAsync();
                var stopWatch = new Stopwatch();
                long bytesFrom;
                long bytesTo;
                long bytesCaptured;
                do
                {
                    bytesFrom = 0;
                    bytesTo = 0;
                    stopWatch.Start();
                    bytesFrom = this.BytesDownloaded;
                    await Task.Delay(1000);
                    bytesTo = this.BytesDownloaded;
                    stopWatch.Stop();
                    bytesCaptured = bytesTo - bytesFrom;
                    if (bytesCaptured >= 0 && stopWatch.ElapsedMilliseconds > 0)
                    {
                        this.Speed = (long)((double)bytesCaptured / ((double)stopWatch.ElapsedMilliseconds / 1000));
                        RaisePropertyChanged(nameof(this.Speed));
                        RaisePropertyChanged(nameof(this.BytesDownloaded));
                    }
                    stopWatch.Reset();
                } while (bytesCaptured > 0);
                this.Speed = null;
                RaisePropertyChanged(nameof(this.Speed));
                _semapreplacedMeasuringSpeed.Release();
            });
        }

19 View Source File : CooldownAttribute.cs
License : MIT License
Project Creator : Aiko-IT-Systems

internal async Task<bool> DecrementUseAsync()
        {
            await this.UsageSemapreplaced.WaitAsync().ConfigureAwait(false);

            // if we're past reset time...
            var now = DateTimeOffset.UtcNow;
            if (now >= this.ResetsAt)
            {
                // ...do the reset and set a new reset time
                Interlocked.Exchange(ref this._remaining_uses, this.MaxUses);
                this.ResetsAt = now + this.Reset;
            }

            // check if we have any uses left, if we do...
            var success = false;
            if (this.RemainingUses > 0)
            {
                // ...decrement, and return success...
                Interlocked.Decrement(ref this._remaining_uses);
                success = true;
            }

            // ...otherwise just fail
            this.UsageSemapreplaced.Release();
            return success;
        }

19 View Source File : ConsulDiscoverDelegatingHandler.cs
License : MIT License
Project Creator : AlphaYu

private async Task<List<string>> GetAllHealthServiceAddressAsync(string serviceName, string serviceAddressCacheKey)
        {
            var healthAddresses = _memoryCache.Get<List<string>>(serviceAddressCacheKey);
            if (healthAddresses != null && healthAddresses.Any())
            {
                return healthAddresses;
            }

            await _slimlock.WaitAsync();

            try
            {
                _logger.LogInformation($"SemapreplacedSlim=true,{serviceAddressCacheKey}");
                healthAddresses = _memoryCache.Get<List<string>>(serviceAddressCacheKey);
                if (healthAddresses != null && healthAddresses.Any())
                {
                    return healthAddresses;
                }
                var query = await _consulClient.Health.Service(serviceName, string.Empty, true);
                var servicesEntries = query.Response;
                if (servicesEntries != null && servicesEntries.Any())
                {
                    var entryOptions = new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5)
                    };
                    healthAddresses = servicesEntries.Select(entry => $"{entry.Service.Address}:{entry.Service.Port}").ToList();
                    _memoryCache.Set(serviceAddressCacheKey, healthAddresses, entryOptions);
                }
                return healthAddresses;
            }
            finally
            {
                _slimlock.Release();
            }
        }

19 View Source File : ChatsViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private async Task LoadGroupsAndChats()
        {
            await this.ReloadGroupsSem.WaitAsync();

            try
            {
                await this.GroupMeClient.GetGroupsAsync();
                await this.GroupMeClient.GetChatsAsync();

                var groupsAndChats = Enumerable.Concat<IMessageContainer>(this.GroupMeClient.Groups(), this.GroupMeClient.Chats());

                foreach (var group in groupsAndChats)
                {
                    // check the last-read message status from peristant storage
                    var groupState = this.SettingsManager.ChatsSettings.GroupChatStates.Find(g => g.GroupOrChatId == group.Id);
                    if (groupState == null)
                    {
                        groupState = new ChatsSettings.GroupOrChatState()
                        {
                            GroupOrChatId = group.Id,
                            LastTotalMessageCount = group.TotalMessageCount,
                        };
                        this.SettingsManager.ChatsSettings.GroupChatStates.Add(groupState);
                    }

                    // Code to update the UI needs to be run on the Application Dispatcher
                    // This is typically the case, but Timer events from ReliabilityStateMachine for
                    // retry-callbacks will NOT run on the original thread.
                    await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
                    {
                        // calculate how many new messages have been added since the group/chat was last read
                        var unreadMessages = group.TotalMessageCount - groupState.LastTotalMessageCount;

                        if (unreadMessages < 0)
                        {
                            // strange errors can occur when the Group Listing lags behind the
                            // actual group contents. If this occurs, cancel and reload the sidebar.
                            this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(async () => await this.LoadGroupsAndChats());
                            return;
                        }

                        var existingVm = this.AllGroupsChats.Items.FirstOrDefault(g => g.Id == group.Id);
                        if (existingVm == null)
                        {
                            // create a new GroupControl ViewModel for this Group
                            var vm = new GroupControlViewModel(group)
                            {
                                GroupSelected = new RelayCommand<GroupControlViewModel>(this.OpenNewGroupChat, (g) => true),
                                TotalUnreadCount = unreadMessages,
                            };
                            this.AllGroupsChats.Add(vm);
                        }
                        else
                        {
                            // Update the existing Group/Chat VM
                            existingVm.MessageContainer = group;
                            existingVm.TotalUnreadCount = unreadMessages;
                        }

                        var openChatGroup = this.ActiveGroupsChats.FirstOrDefault(g => g.Id == group.Id);
                        if (openChatGroup != null)
                        {
                            // chat is open and already receiving new messages, so mark all messages as "read"
                            existingVm.TotalUnreadCount = 0;
                            groupState.LastTotalMessageCount = openChatGroup.MessageContainer.TotalMessageCount;
                        }
                    });
                }

                this.SettingsManager.SaveSettings();
                this.PublishTotalUnreadCount();

                // if everything was successful, reset the reliability monitor
                this.ReliabilityStateMachine.Succeeded();
                this.RetryTimer?.Dispose();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Exception in {nameof(this.LoadGroupsAndChats)} - {ex.Message}. Retrying...");
                this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(async () => await this.LoadGroupsAndChats());
            }
            finally
            {
                this.ReloadGroupsSem.Release();
            }
        }

19 View Source File : Seeder.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task Seed()
        {
            try
            {
                await SemapreplacedSlim.WaitAsync();
                await AllClear();
                var result = await _http.Get(new AiurUrl(_configuration["ResourcesUrl"] + "structure.json"));
                var sourceObject = JsonConvert.DeserializeObject<List<Collection>>(result);

                // Get all collections
                foreach (var collection in sourceObject)
                {
                    // Insert collection
                    var newCollection = new Collection
                    {
                        Collectionreplacedle = collection.Collectionreplacedle,
                        DocAPIAddress = collection.DocAPIAddress
                    };
                    await _dbContext.Collections.AddAsync(newCollection);
                    await _dbContext.SaveChangesAsync();

                    // Get markdown from existing doreplacedents.
                    foreach (var article in collection.Articles ?? new List<Article>())
                    {
                        // markdown http url.
                        if (!string.IsNullOrEmpty(article.ArticleAddress))
                        {
                            var newArticle = new Article
                            {
                                Articlereplacedle = article.Articlereplacedle,
                                ArticleAddress = article.ArticleAddress,
                                ArticleContent = await _http.Get(new AiurUrl(article.ArticleAddress)),
                                CollectionId = newCollection.CollectionId
                            };
                            await _dbContext.Article.AddAsync(newArticle);
                            await _dbContext.SaveChangesAsync();
                        }
                        // GitHub repo.
                        else
                        {
                            var newArticle = new Article
                            {
                                Articlereplacedle = article.Articlereplacedle,
                                ArticleContent = await _http.Get(new AiurUrl($"{_configuration["ResourcesUrl"]}{collection.Collectionreplacedle}/{article.Articlereplacedle}.md")),
                                CollectionId = newCollection.CollectionId
                            };
                            await _dbContext.Article.AddAsync(newArticle);
                            await _dbContext.SaveChangesAsync();
                        }
                    }

                    // Parse the appended doc.
                    if (!string.IsNullOrWhiteSpace(collection.DocAPIAddress))
                    {
                        var doamin = _configuration["RootDomain"];
                        var docBuilt = collection.DocAPIAddress
                            .Replace("{{rootDomain}}", doamin);
                        // Generate markdown from doc generator
                        var docString = await _http.Get(new AiurUrl(docBuilt));
                        var docModel = JsonConvert.DeserializeObject<List<API>>(docString);
                        var docGrouped = docModel.GroupBy(t => t.ControllerName);
                        var apiRoot = docBuilt.ToLower().Replace("/doc", "");
                        foreach (var docController in docGrouped)
                        {
                            var markdown = _markDownGenerator.GenerateMarkDownForAPI(docController, apiRoot);
                            var newArticle = new Article
                            {
                                Articlereplacedle = docController.Key.TrimController(),
                                ArticleContent = markdown,
                                CollectionId = newCollection.CollectionId,
                                BuiltByJson = true
                            };
                            await _dbContext.Article.AddAsync(newArticle);
                            await _dbContext.SaveChangesAsync();
                        }
                    }
                }
            }
            finally
            {
                SemapreplacedSlim.Release();
            }
        }

19 View Source File : DeepL_Website.cs
License : GNU General Public License v3.0
Project Creator : AndrasMumm

public async Task ForceTranslate()
        {
            double spanDiff = MaxDelaySeconds - MinDelaySeconds;
            double waitIntervalMs = MaxDelaySeconds + (RandomNumbers.NextDouble() * spanDiff) * 1000;
            double timeWeNeedToWait = waitIntervalMs - DateTime.Now.Subtract(_LastTimeTranslated).Milliseconds;
            if (timeWeNeedToWait > 0)
            {
                Thread.Sleep((int)Math.Floor(timeWeNeedToWait));
            }

            try
            {
                await _Sem.WaitAsync();
                RequestListManipMutex.WaitOne();
                _CurrentlyTranslating = _Requests;
                _Requests = new List<TranslationRequest>();
                RequestListManipMutex.ReleaseMutex();

                await EnsureSetupState();

                _TranslationCount++;
                _ID++;

                // construct json content
                long r = (long)(DateTime.UtcNow - Epoch).TotalMilliseconds;
                long n = 1;


                var builder = new StringBuilder();
                builder.Append("{\"jsonrpc\":\"2.0\",\"method\": \"LMT_handle_jobs\",\"params\":{\"jobs\":[");

                List<UntranslatedTextInfo> untranslatedTextInfos = new List<UntranslatedTextInfo>();
                foreach (var translationRequest in _CurrentlyTranslating)
                {
                    List<TranslationPart> parts = NewlineSplitter
                       .Split(translationRequest.Text)
                       .Select(x => new TranslationPart { Value = x, IsTranslatable = !NewlineSplitter.IsMatch(x) })
                       .ToList();

                    var usableParts = parts
                        .Where(x => x.IsTranslatable)
                        .Select(x => x.Value)
                        .ToArray();

                    for (int i = 0; i < usableParts.Length; i++)
                    {
                        var usablePart = usableParts[i];

                        builder.Append("{\"kind\":\"default\",\"preferred_num_beams\":1,\"raw_en_sentence\":\""); // yes.. "en" no matter what source language is used
                        builder.Append(JsonHelper.Escape(usablePart));

                        var addedContext = new HashSet<string>();
                        builder.Append("\",\"raw_en_context_before\":[");
                        bool addedAnyBefore = false;
                        foreach (var contextBefore in translationRequest.PreContext)
                        {
                            if (!addedContext.Contains(contextBefore))
                            {
                                builder.Append("\"");
                                builder.Append(JsonHelper.Escape(contextBefore));
                                builder.Append("\"");
                                builder.Append(",");
                                addedAnyBefore = true;
                            }
                        }
                        for (int j = 0; j < i; j++)
                        {
                            if (!addedContext.Contains(usableParts[j]))
                            {
                                builder.Append("\"");
                                builder.Append(JsonHelper.Escape(usableParts[j]));
                                builder.Append("\"");
                                builder.Append(",");
                                addedAnyBefore = true;
                            }
                        }
                        if (addedAnyBefore)
                        {
                            builder.Remove(builder.Length - 1, 1);
                        }

                        builder.Append("],\"raw_en_context_after\":[");
                        bool addedAnyAfter = false;
                        for (int j = i + 1; j < usableParts.Length; j++)
                        {
                            if (!addedContext.Contains(usableParts[j]))
                            {
                                builder.Append("\"");
                                builder.Append(JsonHelper.Escape(usableParts[j]));
                                builder.Append("\"");
                                builder.Append(",");
                                addedAnyAfter = true;
                            }
                        }
                        foreach (var contextAfter in translationRequest.PostContext)
                        {
                            if (!addedContext.Contains(contextAfter))
                            {
                                builder.Append("\"");
                                builder.Append(JsonHelper.Escape(contextAfter));
                                builder.Append("\"");
                                builder.Append(",");
                                addedAnyAfter = true;
                            }
                        }
                        if (addedAnyAfter)
                        {
                            builder.Remove(builder.Length - 1, 1);
                        }
                        //builder.Append("],\"quality\":\"fast\"},");
                        builder.Append("]},");

                        n += usablePart.Count(c => c == 'i');
                    }

                    untranslatedTextInfos.Add(new UntranslatedTextInfo { TranslationParts = parts, UntranslatedText = translationRequest.Text });
                }
                builder.Remove(builder.Length - 1, 1); // remove final ","

                var timestamp = r + (n - r % n);

                builder.Append("],\"lang\":{\"user_preferred_langs\":[\"");
                builder.Append("en".ToUpperInvariant());
                builder.Append("\",\"");
                builder.Append("zh".ToUpperInvariant());
                builder.Append("\"],\"source_lang_user_selected\":\"");
                builder.Append("zh".ToUpperInvariant());
                builder.Append("\",\"target_lang\":\"");
                builder.Append("en".ToUpperInvariant());
                builder.Append("\"},\"priority\":1,\"commonJobParams\":{\"formality\":null},\"timestamp\":");
                builder.Append(timestamp.ToString(CultureInfo.InvariantCulture));
                builder.Append("},\"id\":");
                builder.Append(_ID);
                builder.Append("}");
                var content = builder.ToString();

                var stringContent = new StringContent(content);

                using var request = new HttpRequestMessage(HttpMethod.Post, HttpsServicePointTemplateUrl);
                AddHeaders(request, stringContent, RequestType.Translation);
                request.Content = stringContent;

                // create request
                using var response = await _Client.SendAsync(request);
                response.ThrowIfBlocked();
                response.EnsureSuccessStatusCode();

                var str = await response.Content.ReadreplacedtringAsync();

                ExtractTranslation(str, untranslatedTextInfos);
                _LastTimeTranslated = DateTime.Now;
            }
            catch (BlockedException)
            {
                Reset();

                throw;
            }
            finally
            {
                _Sem.Release();
            }
        }

19 View Source File : WebSocketClient.cs
License : MIT License
Project Creator : Aiko-IT-Systems

public async Task DisconnectAsync(int code = 1000, string message = "")
        {
            // Ensure that messages cannot be sent
            await this._senderLock.WaitAsync().ConfigureAwait(false);

            try
            {
                this._isClientClose = true;
                if (this._ws != null && (this._ws.State == WebSocketState.Open || this._ws.State == WebSocketState.CloseReceived))
                    await this._ws.CloseOutputAsync((WebSocketCloseStatus)code, message, CancellationToken.None).ConfigureAwait(false);

                if (this._receiverTask != null)
                    await this._receiverTask.ConfigureAwait(false); // Ensure that receiving completed

                if (this._isConnected)
                    this._isConnected = false;

                if (!this._isDisposed)
                {
                    // Cancel all running tasks
                    if (this._socketToken.CanBeCanceled)
                        this._socketTokenSource?.Cancel();
                    this._socketTokenSource?.Dispose();

                    if (this._receiverToken.CanBeCanceled)
                        this._receiverTokenSource?.Cancel();
                    this._receiverTokenSource?.Dispose();

                    this._isDisposed = true;
                }
            }
            catch { }
            finally
            {
                this._senderLock.Release();
            }
        }

19 View Source File : AsyncAwaiter.cs
License : MIT License
Project Creator : angelsix

public static async Task<T> AwaitResultAsync<T>(string key, Func<Task<T>> task, int maxAccessCount = 1)
        {
            #region Create Semapreplaced

            //
            // Asynchronously wait to enter the Semapreplaced
            //
            // If no-one has been granted access to the Semapreplaced
            // code execution will proceed
            // Otherwise this thread waits here until the semapreplaced is released 
            //
            await SelfLock.WaitAsync();

            try
            {
                // Create semapreplaced if it doesn't already exist
                if (!Semapreplaceds.ContainsKey(key))
                    Semapreplaceds.Add(key, new SemapreplacedSlim(maxAccessCount, maxAccessCount));
            }
            finally
            {
                //
                // When the task is ready, release the semapreplaced
                //
                // It is vital to ALWAYS release the semapreplaced when we are ready
                // or else we will end up with a Semapreplaced that is forever locked
                // This is why it is important to do the Release within a try...finally clause
                // Program execution may crash or take a different path, this way you are guaranteed execution
                //
                SelfLock.Release();
            }

            #endregion

            // Now use this semapreplaced and perform the desired task inside its lock
            // NOTE: We never remove semapreplaceds after creating them, so this will never be null
            var semapreplaced = Semapreplaceds[key];

            // Await this semapreplaced
            await semapreplaced.WaitAsync();

            try
            {
                // Perform the job
                return await task();
            }
            finally
            {
                // Release the semapreplaced
                semapreplaced.Release();
            }
        }

19 View Source File : RecordRepo.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task<WarpRecord> CreateRecord(string newRecordName, RecordType type, string appid, string targetUrl, bool enabled, string tags)
        {
            await _createRecordLock.WaitAsync();
            try
            {
                await _table.EnsureUniqueString(t => t.RecordUniqueName, newRecordName);
                var newRecord = new WarpRecord
                {
                    RecordUniqueName = newRecordName.ToLower(),
                    Type = type,
                    AppId = appid,
                    TargetUrl = targetUrl,
                    Enabled = enabled,
                    Tags = tags
                };
                await _table.AddAsync(newRecord);
                await _dbContext.SaveChangesAsync();
                return newRecord;
            }
            finally
            {
                _createRecordLock.Release();
            }
        }

19 View Source File : DownloaderObjectModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

public async Task StartAsync()
        {
            await _semapreplacedDownloading.WaitAsync();
            try
            {
                long bytesAlreadyDownloaded = 0;
                if (_ctsPaused != null)
                {
                    // Download in progress
                    return;
                }
                else if (_ctsCanceled != null || (_ctsPaused == null && _ctsCanceled == null))
                {
                    if (this.IsCompleted)
                    {
                        // Don't start an already completed download
                        SetFinished();
                        return;
                    }
                    else
                    {
                        this.Status = DownloadStatus.Connecting;
                        RaisePropertyChanged(nameof(this.Status));
                        UrlVerificationModel urlVerification;
                        urlVerification = await VerifyUrlAsync(this.Url);

                        // Ensure url is valid for all downloads
                        if (!IsValidStatus(urlVerification.StatusCode))
                        {
                            SetErrored();
                            return;
                        }

                        if (this.IsPaused)
                        {
                            // Download is paused; resume from specific point
                            bytesAlreadyDownloaded = new FileInfo(TempDestination).Length;
                        }
                        else
                        {
                            if (!CreateEmptyDownload(this.Destination))
                            {
                                throw new IOException("Failed to create new download.");
                            }
                        }
                    }
                }

                _taskCompletion = new TaskCompletionSource<DownloadStatus>(ProcessStreamsAsync(bytesAlreadyDownloaded));
                this.BytesDownloadedThisSession = 0;
                this.TotalBytesCompleted = bytesAlreadyDownloaded;
                this.Status = DownloadStatus.Downloading;
                RaisePropertyChanged(nameof(this.Status));
                RaisePropertyChanged(nameof(this.TotalBytesCompleted));
                RaisePropertyChanged(nameof(this.IsBeingDownloaded));

                RaiseEvent(DownloadStarted);
                var result = await _taskCompletion.Task;

                switch (result)
                {
                    case DownloadStatus.Error:
                        this.Cancel();
                        SetErrored();
                        break;

                    case DownloadStatus.Ready:
                        this.Cancel();
                        SetReady();
                        break;

                    case DownloadStatus.Paused:
                        this.Pause();
                        SetPaused();
                        break;

                    case DownloadStatus.Finished:
                        this.Status = DownloadStatus.Finishing;
                        RaisePropertyChanged(nameof(this.Status));
                        int retry = 5;
                        while (retry-- > 0)
                        {
                            try
                            {
                                File.Move(TempDestination, this.Destination, true);
                                SetFinished();
                                break;
                            }
                            catch
                            {
                                await Task.Delay(1000);
                                continue;
                            }
                        }
                        break;

                    default:
                        this.Cancel();
                        SetReady();
                        break;
                }

                RaisePropertyChanged(nameof(this.IsBeingDownloaded));
                RaiseEvent(DownloadStopped);
                if (result == DownloadStatus.Finished) RaiseEvent(DownloadFinished);
            }
            finally
            {
                _semapreplacedDownloading.Release();
            }
        }

19 View Source File : WebSocketPlayController.cs
License : MIT License
Project Creator : a1q123456

private async Task PlayRecordFile()
        {
            Interlocked.Exchange(ref _playing, 1);
            var buffer = new byte[512];
            int bytesRead;
            do
            {
                await _playLock.WaitAsync();
                bytesRead = await _recordFile.ReadAsync(buffer);
                await Session.SendRawDataAsync(buffer);
                _playLock.Release();
                if (_playRangeTo < _recordFile.Position && _playRangeTo != -1)
                {
                    break;
                }
            } while (bytesRead != 0);
            Interlocked.Exchange(ref _playing, 0);
        }

19 View Source File : DownloaderViewModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

private async Task RemoveObjectsAsync(bool delete, params DownloaderObjectModel[] objects)
        {
            await _semapreplacedUpdatingList.WaitAsync();

            var dequeueThese = new List<IQueueable>();
            var itemsProcessed = new List<DownloaderObjectModel>();
            var total = objects.Count();
            _ctsUpdatingList = new CancellationTokenSource();
            var ct = _ctsUpdatingList.Token;
            RaisePropertyChanged(nameof(this.IsBackgroundWorking));
            int progress;
            string primaryStatus = "Removing ";
            if (delete) primaryStatus = "Deleting ";
            for (int i = 0; i < total; i++)
            {
                progress = (int)((double)(i + 1) / total * 100);
                this.Status = primaryStatus + (i + 1) + " of " + total + ": " + objects[i].Name;
                this.Progress = progress;
                RaisePropertyChanged(nameof(this.Status));
                RaisePropertyChanged(nameof(this.Progress));

                if (objects[i] == null) continue;

                if (objects[i].IsQueued)
                {
                    objects[i].Dequeue();
                    dequeueThese.Add(objects[i]);
                }

                if (objects[i].IsBeingDownloaded)
                {
                    await objects[i].CancelAsync();
                }
                else
                {
                    // delete all UNFINISHED downloads forcefully
                    if (objects[i].Status != DownloadStatus.Finished || delete)
                    {
                        try
                        {
                            if (objects[i].Status == DownloadStatus.Finished)
                            {
                                FileSystem.DeleteFile(
                                    objects[i].Destination,
                                    UIOption.OnlyErrorDialogs,
                                    RecycleOption.SendToRecycleBin);
                            }
                            else
                            {
                                File.Delete(objects[i].TempDestination);
                            }
                        }
                        catch { }
                    }
                }

                itemsProcessed.Add(objects[i]);

                if (ct.IsCancellationRequested)
                {
                    break;
                }
            }

            this.Status = "Delisting...";
            RaisePropertyChanged(nameof(this.Status));

            Application.Current.Dispatcher.Invoke(() =>
            {
                Monitor.Enter(_lockDownloadItemsList);
                for (int i = 0; i < itemsProcessed.Count(); i++)
                {
                    DownloadItemsList.Remove(itemsProcessed[i]);
                }
                Monitor.Exit(_lockDownloadItemsList);
            });

            if (dequeueThese.Count > 0)
            {
                this.Status = "Refreshing queue...";
                RaisePropertyChanged(nameof(this.Status));
                QueueProcessor.Remove(dequeueThese.ToArray());
            }

            _ctsUpdatingList = null;
            this.Status = "Ready";
            this.Progress = 0;
            RaisePropertyChanged(nameof(this.Status));
            RaisePropertyChanged(nameof(this.Progress));
            RaisePropertyChanged(nameof(this.IsBackgroundWorking));
            _semapreplacedUpdatingList.Release();
        }

19 View Source File : FormProvider.cs
License : MIT License
Project Creator : alex-oswald

public async Task<T> GetFormAsync<T>()
            where T : Form
        {
            // We are throttling this because there is only one gui thread
            await _semapreplaced.WaitAsync();

            var form = await SynchronizationContext.InvokeAsync(() => _serviceProvider.GetService<T>());

            _semapreplaced.Release();

            return form;
        }

19 View Source File : ChatsViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private async Task LoadGroupsAndChats(bool updateSuperIndexer = false)
        {
            await this.ReloadGroupsSem.WaitAsync();

            var persistContext = this.PersistManager.OpenNewContext();

            try
            {
                await this.GroupMeClient.GetGroupsAsync();
                await this.GroupMeClient.GetChatsAsync();

                var groupsAndChats = Enumerable.Concat<IMessageContainer>(this.GroupMeClient.Groups(), this.GroupMeClient.Chats());

                if (updateSuperIndexer)
                {
                    // Submit new Group and Chat listings to SuperIndexer to allow for background indexing if needed.
                    this.CacheManager.SuperIndexer.BeginAsyncTransaction(groupsAndChats);
                }

                foreach (var group in groupsAndChats)
                {
                    // check the last-read message status from peristant storage
                    var groupState = persistContext.GroupChatStates.FirstOrDefault(g => g.GroupOrChatId == group.Id);
                    if (groupState == null)
                    {
                        groupState = new GroupOrChatState()
                        {
                            GroupOrChatId = group.Id,
                            LastTotalMessageCount = group.TotalMessageCount,
                        };
                        persistContext.GroupChatStates.Add(groupState);
                    }

                    // Code to update the UI needs to be run on the Application Dispatcher
                    // This is typically the case, but Timer events from ReliabilityStateMachine for
                    // retry-callbacks will NOT run on the original thread.
                    var uiDispatcher = Ioc.Default.GetService<IUserInterfaceDispatchService>();
                    await uiDispatcher.InvokeAsync(() =>
                    {
                        // calculate how many new messages have been added since the group/chat was last read
                        var unreadMessages = group.TotalMessageCount - groupState.LastTotalMessageCount;

                        if (unreadMessages < 0)
                        {
                            // strange errors can occur when the Group Listing lags behind the
                            // actual group contents. If this occurs, cancel and reload the sidebar.
                            this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(() => Task.Run(() => this.LoadGroupsAndChats()));
                            return;
                        }

                        var existingVm = this.AllGroupsChats.Items.FirstOrDefault(g => g.Id == group.Id);
                        if (existingVm == null)
                        {
                            // create a new GroupControl ViewModel for this Group
                            var vm = new GroupControlViewModel(group)
                            {
                                GroupSelected = new RelayCommand<GroupControlViewModel>((g) => this.OpenNewGroupChat(g)),
                                TotalUnreadCount = unreadMessages,
                            };
                            this.AllGroupsChats.Add(vm);
                        }
                        else
                        {
                            // Update the existing Group/Chat VM
                            existingVm.MessageContainer = group;
                            existingVm.TotalUnreadCount = unreadMessages;
                        }

                        var openChatGroup = this.AllChats.FirstOrDefault(g => g.Id == group.Id);
                        if (openChatGroup != null)
                        {
                            // chat is open and already receiving new messages, so mark all messages as "read"
                            existingVm.TotalUnreadCount = 0;
                            groupState.LastTotalMessageCount = openChatGroup.MessageContainer.TotalMessageCount;
                        }
                    });
                }

                await persistContext.SaveChangesAsync();
                this.PublishTotalUnreadCount();

                // if everything was successful, reset the reliability monitor
                this.ReliabilityStateMachine.Succeeded();
                this.RetryTimer?.Dispose();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Exception in {nameof(this.LoadGroupsAndChats)} - {ex.Message}. Retrying...");
                this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(() => Task.Run(() => this.LoadGroupsAndChats()));
            }
            finally
            {
                persistContext.Dispose();
                this.ReloadGroupsSem.Release();
            }
        }

19 View Source File : ChunkStreamContext.cs
License : MIT License
Project Creator : a1q123456

internal async Task MultiplexMessageAsync(uint chunkStreamId, Message message)
        {
            if (!message.MessageHeader.MessageStreamId.HasValue)
            {
                throw new InvalidOperationException("cannot send message that has not attached to a message stream");
            }
            byte[] buffer = null;
            uint length = 0;
            using (var writeBuffer = new ByteBuffer())
            {
                var context = new Serialization.SerializationContext()
                {
                    Amf0Reader = _amf0Reader,
                    Amf0Writer = _amf0Writer,
                    Amf3Reader = _amf3Reader,
                    Amf3Writer = _amf3Writer,
                    WriteBuffer = writeBuffer
                };
                message.Serialize(context);
                length = (uint)writeBuffer.Length;
                Debug.replacedert(length != 0);
                buffer = _arrayPool.Rent((int)length);
                writeBuffer.TakeOutMemory(buffer);
            }

            try
            {
                message.MessageHeader.MessageLength = length;
                Debug.replacedert(message.MessageHeader.MessageLength != 0);
                if (message.MessageHeader.MessageType == 0)
                {
                    message.MessageHeader.MessageType = message.GetType().GetCustomAttribute<RtmpMessageAttribute>().MessageTypes.First();
                }
                Debug.replacedert(message.MessageHeader.MessageType != 0);
                Task ret = null;
                // chunking
                bool isFirstChunk = true;
                _rtmpSession.replacedertStreamId(message.MessageHeader.MessageStreamId.Value);
                for (int i = 0; i < message.MessageHeader.MessageLength;)
                {
                    _previousWriteMessageHeader.TryGetValue(chunkStreamId, out var prevHeader);
                    var chunkHeaderType = SelectChunkType(message.MessageHeader, prevHeader, isFirstChunk);
                    isFirstChunk = false;
                    GenerateBasicHeader(chunkHeaderType, chunkStreamId, out var basicHeader, out var basicHeaderLength);
                    GenerateMesesageHeader(chunkHeaderType, message.MessageHeader, prevHeader, out var messageHeader, out var messageHeaderLength);
                    _previousWriteMessageHeader[chunkStreamId] = (MessageHeader)message.MessageHeader.Clone();
                    var headerLength = basicHeaderLength + messageHeaderLength;
                    var bodySize = (int)(length - i >= _writeChunkSize ? _writeChunkSize : length - i);

                    var chunkBuffer = _arrayPool.Rent(headerLength + bodySize);
                    await _sync.WaitAsync();
                    try
                    {
                        basicHeader.replacedpan(0, basicHeaderLength).CopyTo(chunkBuffer);
                        messageHeader.replacedpan(0, messageHeaderLength).CopyTo(chunkBuffer.replacedpan(basicHeaderLength));
                        _arrayPool.Return(basicHeader);
                        _arrayPool.Return(messageHeader);
                        buffer.replacedpan(i, bodySize).CopyTo(chunkBuffer.replacedpan(headerLength));
                        i += bodySize;
                        var isLastChunk = message.MessageHeader.MessageLength - i == 0;

                        long offset = 0;
                        long totalLength = headerLength + bodySize;
                        long currentSendSize = totalLength;

                        while (offset != (headerLength + bodySize))
                        {
                            if (WriteWindowAcknowledgementSize.HasValue && Interlocked.Read(ref WriteUnAcknowledgedSize) + headerLength + bodySize > WriteWindowAcknowledgementSize.Value)
                            {
                                currentSendSize = Math.Min(WriteWindowAcknowledgementSize.Value, currentSendSize);
                                //var delayCount = 0;
                                while (currentSendSize + Interlocked.Read(ref WriteUnAcknowledgedSize) >= WriteWindowAcknowledgementSize.Value)
                                {
                                    await Task.Delay(1);
                                }
                            }
                            var tsk = _ioPipeline.SendRawData(chunkBuffer.AsMemory((int)offset, (int)currentSendSize));
                            offset += currentSendSize;
                            totalLength -= currentSendSize;

                            if (WriteWindowAcknowledgementSize.HasValue)
                            {
                                Interlocked.Add(ref WriteUnAcknowledgedSize, currentSendSize);
                            }
                            
                            if (isLastChunk)
                            {
                                ret = tsk;
                            }
                        }
                        if (isLastChunk)
                        {
                            if (message.MessageHeader.MessageType == MessageType.SetChunkSize)
                            {
                                var setChunkSize = message as SetChunkSizeMessage;
                                _writeChunkSize = setChunkSize.ChunkSize;
                            }
                            else if (message.MessageHeader.MessageType == MessageType.SetPeerBandwidth)
                            {
                                var m = message as SetPeerBandwidthMessage;
                                ReadWindowAcknowledgementSize = m.WindowSize;
                            }
                            else if (message.MessageHeader.MessageType == MessageType.WindowAcknowledgementSize)
                            {
                                var m = message as WindowAcknowledgementSizeMessage;
                                WriteWindowAcknowledgementSize = m.WindowSize;
                            }
                        }
                    }
                    finally
                    {
                        _sync.Release();
                        _arrayPool.Return(chunkBuffer);
                    }
                }
                Debug.replacedert(ret != null);
                await ret;

            }
            finally
            {
                _arrayPool.Return(buffer);
            }

        }

19 View Source File : GroupContentsControlViewModel.cs
License : GNU General Public License v3.0
Project Creator : alexdillon

private async Task LoadMoreAsync(ScrollViewer scrollViewer = null, bool updateNewest = false)
        {
            await this.ReloadSem.WaitAsync();

            try
            {
                ICollection<Message> results;

                if (this.FirstDisplayedMessage == null || updateNewest)
                {
                    // load the most recent messages
                    results = await this.MessageContainer.GetMessagesAsync();
                }
                else
                {
                    // load the 20 (GroupMe default) messages preceeding the oldest (first) one displayed
                    results = await this.MessageContainer.GetMessagesAsync(GroupMeClientApi.MessageRetreiveMode.BeforeId, this.FirstDisplayedMessage.Id);
                }

                this.UpdateDisplay(scrollViewer, results);

                // if everything was successful, reset the reliability monitor
                this.ReliabilityStateMachine.Succeeded();
                this.RetryTimer?.Dispose();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"Exception in {nameof(this.LoadMoreAsync)} - {ex.Message}. Retrying...");
                this.RetryTimer = this.ReliabilityStateMachine.GetRetryTimer(async () => await this.LoadMoreAsync(scrollViewer, updateNewest));
            }
            finally
            {
                this.ReloadSem.Release();
            }
        }

19 View Source File : Remote.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task PushAsync()
        {
            await PushLock.WaitAsync();
            var commitsToPush = ContextRepository.Commits.GetAllAfter(PushPointer).ToList();
            if (commitsToPush.Any())
            {
                var uploaded = await ConnectionProvider.Upload(commitsToPush, PushPointer?.Id);
                if(uploaded)
                {
                    PushPointer = commitsToPush.Last();
                }
            }
            PushLock.Release();
        }

19 View Source File : Program.cs
License : MIT License
Project Creator : allantargino

static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            await _semapreplaced.WaitAsync();
            try
            {
                _lastExecution = DateTime.Now;
                Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
                Console.Out.Flush();
                await _client.CompleteAsync(message.SystemProperties.LockToken);
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

19 View Source File : AsyncLock.cs
License : MIT License
Project Creator : alexrainman

public Task<IDisposable> LockAsync()
        {
            var wait = m_semapreplaced.WaitAsync();
            return wait.IsCompleted ?
                m_releaser :
                wait.ContinueWith((_, state) => (IDisposable)state,
                    m_releaser.Result, CancellationToken.None,
                    TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
        }

19 View Source File : Remote.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task StopMonitoring()
        {
            await PushLock.WaitAsync();
            await ConnectionProvider.Disconnect();
            PushLock.Release();
        }

19 View Source File : JwtSigningCertificateProvider.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

public async Task<List<X509Certificate2>> GetCertificates()
        {
            await _semapreplaced.WaitAsync();

            try
            {
                if (_certificateUpdateTime > DateTime.Now && _certificates != null)
                {
                    return _certificates;
                }

                _certificates = new List<X509Certificate2>();

                if (string.IsNullOrEmpty(_keyVaultSettings.ClientId) || string.IsNullOrEmpty(_keyVaultSettings.ClientSecret))
                {
                    _certificates.Add(new X509Certificate2(_certificateSettings.CertificatePath, _certificateSettings.CertificatePwd));
                }
                else
                {
                    List<X509Certificate2> certificates = await GetAllCertificateVersions(
                        _keyVaultSettings.SecretUri, _certificateSettings.CertificateName);
                    _certificates.AddRange(certificates);
                }

                // Reuse the same list of certificates for 1 hour.
                _certificateUpdateTime = DateTime.Now.AddHours(1);

                _certificates = _certificates.OrderByDescending(cer => cer.NotBefore).ToList();
                return _certificates;
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

19 View Source File : SocketLock.cs
License : MIT License
Project Creator : Aiko-IT-Systems

public async Task LockAsync()
        {
            await this.LockSemapreplaced.WaitAsync().ConfigureAwait(false);

            this.TimeoutCancelSource = new CancellationTokenSource();
            this.UnlockTask = Task.Delay(TimeSpan.FromSeconds(30), this.TimeoutCancel);
            _ = this.UnlockTask.ContinueWith(this.InternalUnlock, TaskContinuationOptions.NotOnCanceled);
        }

19 View Source File : SasTokenProvider.cs
License : BSD 3-Clause "New" or "Revised" License
Project Creator : Altinn

public async Task<string> GetSasToken(string org)
        {
            (DateTime created, string token) sasToken;
            if (_sasTokens.TryGetValue(org, out sasToken) && StillYoung(sasToken.created))
            {
                return sasToken.token;
            }

            _sasTokens.TryRemove(org, out _);

            await _semapreplaced.WaitAsync();
            try
            {
                if (_sasTokens.TryGetValue(org, out sasToken))
                {
                    return sasToken.token;
                }

                string storageAccount = string.Format(_storageConfiguration.OrgStorageAccount, org);
                string sasDefinition = string.Format(_storageConfiguration.OrgSasDefinition, org);
                
                string secretName = $"{storageAccount}-{sasDefinition}";
                string keyVaultUri = string.Format(_storageConfiguration.OrgKeyVaultURI, org);

                _logger.LogInformation($"Getting secret '{secretName}' from '{keyVaultUri}'.");

                (DateTime created, string token) newSasToken = default;
                newSasToken.created = DateTime.UtcNow;
                newSasToken.token = await _keyVaultWrapper.GetSecretAsync(keyVaultUri, secretName);

                _sasTokens.TryAdd(org, newSasToken);

                return newSasToken.token;
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

19 View Source File : Remote.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task<Remote<T>> AttachAsync(IRepository<T> target, bool monitorInCurrentThread = false)
        {
            if (ContextRepository != null)
            {
                throw new InvalidOperationException("You can't attach a remote to more than one repository. Consider creating a new remote!");
            }
            ContextRepository = target;
            if (AutoPush)
            {
                AutoPushsubscription = ContextRepository.AppendCommitsHappened.Subscribe(_ => PushAsync());
            }
            if (AutoPull)
            {
                await ConnectionProvider.PullAndMonitor(onData: async data =>
                {
                    await PullLock.WaitAsync();
                    ContextRepository.OnPulled(data.ToList(), this);
                    PullLock.Release();
                }, () => PullPointer?.Id, onConnected: () => AutoPush ? PushAsync() : Task.CompletedTask, monitorInCurrentThread);
            }
            return this;
        }

19 View Source File : AsyncLock.cs
License : MIT License
Project Creator : angelsix

public static async Task<T> LockResultAsync<T>(string key, Func<Task<T>> task, int maxAccessCount = 1)
        {
            #region Create Semapreplaced

            //
            // Asynchronously wait to enter the Semapreplaced
            //
            //      If no-one has been granted access to the Semapreplaced
            //      code execution will proceed
            //
            //      Otherwise this thread waits here until the semapreplaced is released 
            //
            await SelfLock.WaitAsync();

            try
            {
                // If the semapreplaced with this key does not exist...
                if (!Semapreplaceds.ContainsKey(key))
                    // Create it
                    Semapreplaceds.Add(key, new SemapreplacedDetails(key, maxAccessCount));
            }
            finally
            {
                //
                // When the task is ready, release the semapreplaced
                //
                //      It is vital to ALWAYS release the semapreplaced when we are ready
                //      or else we will end up with a Semapreplaced that is forever locked
                //      This is why it is important to do the Release within a try...finally clause
                //      Program execution may crash or take a different path, this way you are guaranteed execution
                //
                SelfLock.Release();
            }

            #endregion

            // Now use this semapreplaced and perform the desired task inside its lock
            // NOTE: We never remove semapreplaceds after creating them, so this will never be null
            var semapreplaced = Semapreplaceds[key];

            // Await this semapreplaced
            await semapreplaced.Semapreplaced.WaitAsync();

            try
            {
                // Perform the job
                return await task();
            }
            finally
            {
                // Release the semapreplaced
                semapreplaced.Semapreplaced.Release();
            }
        }

19 View Source File : RecordStream.cs
License : MIT License
Project Creator : a1q123456

private async Task SeekAndPlay(double milliSeconds, CancellationToken ct)
        {
            await _playLock.WaitAsync();
            Interlocked.Exchange(ref _playing, 1);
            try
            {

                _recordFile.Seek(9, SeekOrigin.Begin);
                FlvDemuxer.SeekNoLock(milliSeconds, _metaData == null ? null : _metaData.Data[2] as Dictionary<string, object>, ct);
                await StartPlayNoLock(ct);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                Interlocked.Exchange(ref _playing, 0);
                _playLock.Release();
            }
        }

19 View Source File : AsyncAwaitor.cs
License : MIT License
Project Creator : angelsix

public static async Task AwaitAsync(string key, Func<Task> task, int maxAccessCount = 1)
        {
            #region Create Semapreplaced

            //
            // Asynchronously wait to enter the Semapreplaced
            //
            //      If no-one has been granted access to the Semapreplaced
            //      code execution will proceed
            //      Otherwise this thread waits here until the semapreplaced is released 
            //
            await SelfLock.WaitAsync();

            try
            {
                // Create semapreplaced if it doesn't already exist
                if (!Semapreplaceds.ContainsKey(key))
                    Semapreplaceds.Add(key, new SemapreplacedDetails(key, maxAccessCount));
            }
            finally
            {
                //
                // When the task is ready, release the semapreplaced
                //
                //      It is vital to ALWAYS release the semapreplaced when we are ready
                //      or else we will end up with a Semapreplaced that is forever locked
                //      This is why it is important to do the Release within a try...finally clause
                //      Program execution may crash or take a different path, this way you are guaranteed execution
                //
                SelfLock.Release();
            }

            #endregion

            // Now use this semapreplaced and perform the desired task inside its lock
            // NOTE: We never remove semapreplaceds after creating them, so this will never be null
            var semapreplaced = Semapreplaceds[key];

            // Await this semapreplaced
            await semapreplaced.Semapreplaced.WaitAsync();

            try
            {
                // Perform the job
                await task();
            }
            finally
            {
                // Release the semapreplaced
                semapreplaced.Semapreplaced.Release();
            }
        }

19 View Source File : FriendshipController.cs
License : MIT License
Project Creator : AiursoftWeb

private async Task<PrivateConversation> AcceptRequest(Request request, bool accept)
        {
            PrivateConversation newConversation = null;
            request.Completed = true;
            await semapreplacedSlim.WaitAsync();
            try
            {
                if (accept)
                {
                    if (await _dbContext.AreFriends(request.CreatorId, request.TargetId))
                    {
                        await _dbContext.SaveChangesAsync();
                        throw new AiurAPIModelException(ErrorType.HreplaceduccessAlready, "You two are already friends.");
                    }
                    newConversation = _dbContext.AddFriend(request.CreatorId, request.TargetId);
                }
                await _dbContext.SaveChangesAsync();
            }
            finally
            {
                semapreplacedSlim.Release();
            }
            await Task.WhenAll(
                    _pusher.FriendsChangedEvent(
                        request.Creator,
                        request,
                        accept,
                        newConversation?.Build(request.CreatorId, _onlineJudger) as PrivateConversation),
                    _pusher.FriendsChangedEvent(
                        request.Target,
                        request,
                        accept,
                        newConversation?.Build(request.TargetId, _onlineJudger) as PrivateConversation));
            return newConversation;
        }

19 View Source File : AsyncAwaiter.cs
License : MIT License
Project Creator : angelsix

public static async Task AwaitAsync(string key, Func<Task> task, int maxAccessCount = 1)
        {
            #region Create Semapreplaced

            //
            // Asynchronously wait to enter the Semapreplaced
            //
            // If no-one has been granted access to the Semapreplaced
            // code execution will proceed
            // Otherwise this thread waits here until the semapreplaced is released 
            //
            await SelfLock.WaitAsync();

            try
            {
                // Create semapreplaced if it doesn't already exist
                if (!Semapreplaceds.ContainsKey(key))
                    Semapreplaceds.Add(key, new SemapreplacedSlim(maxAccessCount, maxAccessCount));
            }
            finally
            {
                //
                // When the task is ready, release the semapreplaced
                //
                // It is vital to ALWAYS release the semapreplaced when we are ready
                // or else we will end up with a Semapreplaced that is forever locked
                // This is why it is important to do the Release within a try...finally clause
                // Program execution may crash or take a different path, this way you are guaranteed execution
                //
                SelfLock.Release();
            }

            #endregion

            // Now use this semapreplaced and perform the desired task inside its lock
            // NOTE: We never remove semapreplaceds after creating them, so this will never be null
            var semapreplaced = Semapreplaceds[key];

            // Await this semapreplaced
            await semapreplaced.WaitAsync();

            try
            {
                // Perform the job
                await task();
            }
            catch (Exception ex)
            {
                // Get error message
                var error = ex.Message;

                // Log message to debug level 
                // (may not be an issue but we don't want to miss anything in debug)
                Logger.LogDebugSource($"Crash in {nameof(AwaitAsync)}. {ex.Message}");

                // Break debugger
                Debugger.Break();

                // Bubble exception up as normal
                throw;
            }
            finally
            {
                // Release the semapreplaced
                semapreplaced.Release();
            }
        }

19 View Source File : SocketLock.cs
License : MIT License
Project Creator : Aiko-IT-Systems

public Task WaitAsync()
            => this.LockSemapreplaced.WaitAsync();

19 View Source File : DownloaderObjectModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

private async Task<DownloadStatus> ProcessStreamsAsync(long bytesDownloadedPreviously)
        {
            var status = DownloadStatus.Error;
            Task<bool> streamTask;
            HttpRequestMessage request;
            long maxDownloadSpeed = Settings.Default.MaxDownloadSpeed * 1024;
            SemapreplacedSlim semapreplacedProgress = new SemapreplacedSlim(1);
            IProgress<int> streamProgress = new Progress<int>(async (value) =>
            {
                await semapreplacedProgress.WaitAsync();
                this.BytesDownloadedThisSession += value;
                this.TotalBytesCompleted = this.BytesDownloadedThisSession + bytesDownloadedPreviously;
                if (!this.SupportsResume)
                {
                    this.TotalBytesToDownload = this.TotalBytesCompleted;
                }
                _reportBytesProgress.Report(value);
                double progress = (double)this.TotalBytesCompleted / (double)this.TotalBytesToDownload * 100;
                this.Progress = (int)progress;
                semapreplacedProgress.Release();
            });

            // doesn't support multiple streams or each steam under 1 MB
            if (!this.SupportsResume)
            {
                request = new HttpRequestMessage
                {
                    RequestUri = new Uri(this.Url),
                    Method = HttpMethod.Get
                };
            }
            else
            {
                // Set up the request
                request = new HttpRequestMessage
                {
                    RequestUri = new Uri(this.Url),
                    Method = HttpMethod.Get,
                    Headers = { Range = new RangeHeaderValue(bytesDownloadedPreviously, this.TotalBytesToDownload) }
                };
            }

            _ctsPaused = new CancellationTokenSource();
            _ctsCanceled = new CancellationTokenSource();
            _ctPause = _ctsPaused.Token;
            _ctCancel = _ctsCanceled.Token;
            var linkedToken = CancellationTokenSource.CreateLinkedTokenSource(_ctPause, _ctCancel).Token;

            // Process the request
            streamTask = Task.Run(async () =>
            {
                try
                {
                    if (!Directory.Exists(Path.GetDirectoryName(this.Destination)))
                    {
                        throw new AMDownloaderException("Destination directory does not exist.");
                    }
                    if (!File.Exists(TempDestination) && File.Exists(this.Destination))
                    {
                        throw new AMDownloaderException("A new download has not been created.");
                    }

                    using var destinationStream = new FileStream(TempDestination, FileMode.Append, FileAccess.Write);
                    using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                    using var sourceStream = await response.Content.ReadreplacedtreamAsync();
                    using var binaryWriter = new BinaryWriter(destinationStream);
                    byte[] buffer = new byte[AppConstants.DownloaderStreamBufferLength];
                    int s_bytesReceived = 0;
                    int read;
                    var stopWatch = new Stopwatch();
                    while (true)
                    {
                        linkedToken.ThrowIfCancellationRequested();

                        stopWatch.Start();
                        read = await sourceStream.ReadAsync(buffer, 0, buffer.Length, linkedToken);
                        if (read == 0)
                        {
                            request.Dispose();
                            return true;
                        }
                        else
                        {
                            byte[] data = new byte[read];
                            buffer.ToList().CopyTo(0, data, 0, read);
                            binaryWriter.Write(data, 0, data.Length);
                            s_bytesReceived += read;
                            streamProgress.Report(data.Length);
                        }

                        stopWatch.Stop();

                        // Speed throttler
                        if (maxDownloadSpeed > 0 && stopWatch.ElapsedMilliseconds > 0)
                        {
                            int s_bytesExpected = (int)((double)maxDownloadSpeed / 1000 * stopWatch.ElapsedMilliseconds);
                            if (s_bytesReceived > s_bytesExpected)
                            {
                                long expectedMilliseconds = (long)(1000 / (double)maxDownloadSpeed * s_bytesReceived);
                                long delay = expectedMilliseconds - stopWatch.ElapsedMilliseconds;
                                if (delay > 0) await Task.Delay((int)delay);
                                s_bytesReceived = 0;
                                stopWatch.Reset();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new AMDownloaderException(ex.Message, ex);
                }
            });

            StartReportingProgress();
            StartMeasuringEta();

            try
            {
                // Run the tasks
                var finished = await streamTask;

                // Operation complete; verify state
                // completed successfully
                if (finished)
                {
                    status = DownloadStatus.Finished;
                }
                else
                {
                    status = DownloadStatus.Error;
                }
            }
            catch
            {
                // Paused, cancelled or errored
                if (!linkedToken.IsCancellationRequested)
                {
                    status = DownloadStatus.Error;
                }
                else if (_ctPause.IsCancellationRequested)
                {
                    status = DownloadStatus.Paused;
                }
                else if (_ctCancel.IsCancellationRequested)
                {
                    status = DownloadStatus.Ready;
                }
            }

            // Update final size
            if (!this.SupportsResume) this.TotalBytesToDownload = this.TotalBytesCompleted;

            _ctsPaused = null;
            _ctsCanceled = null;
            _ctPause = default;
            _ctCancel = default;
            _taskCompletion.SetResult(status);
            return status;
        }

19 View Source File : FriendshipController.cs
License : MIT License
Project Creator : AiursoftWeb

[HttpPost]
        [APIProduces(typeof(AiurValue<int>))]
        public async Task<IActionResult> CreateRequest([Required] string id)
        {
            var user = await GetKahlaUser();
            await _dbContext.Entry(user)
                .Collection(t => t.HisDevices)
                .LoadAsync();
            var target = await _dbContext.Users.Include(t => t.HisDevices).SingleOrDefaultAsync(t => t.Id == id);
            if (target == null)
            {
                return this.Protocol(ErrorType.NotFound, "We can not find your target user!");
            }
            if (target.Id == user.Id)
            {
                return this.Protocol(ErrorType.Conflict, "You can't request yourself!");
            }
            var areFriends = await _dbContext.AreFriends(user.Id, target.Id);
            if (areFriends)
            {
                return this.Protocol(ErrorType.Conflict, "You two are already friends!");
            }
            Request request;
            await semapreplacedSlim.WaitAsync();
            try
            {
                var pending = await _dbContext.Requests
                    .Where(t =>
                        t.CreatorId == user.Id && t.TargetId == target.Id ||
                        t.CreatorId == target.Id && t.TargetId == user.Id)
                    .AnyAsync(t => !t.Completed);
                if (pending)
                {
                    return this.Protocol(ErrorType.Conflict, "There are some pending request hasn't been completed!");
                }
                request = new Request
                {
                    CreatorId = user.Id,
                    Creator = user,
                    TargetId = id,
                };
                await _dbContext.Requests.AddAsync(request);
                await _dbContext.SaveChangesAsync();
            }
            finally
            {
                semapreplacedSlim.Release();
            }
            await Task.WhenAll(
                _pusher.NewFriendRequestEvent(target, request),
                _pusher.NewFriendRequestEvent(user, request)
            );
            if (_configuration["AutoAcceptRequests"] == true.ToString().ToLower())
            {
                await AcceptRequest(request, true);
            }
            return this.Protocol(new AiurValue<int>(request.Id)
            {
                Code = ErrorType.Success,
                Message = "Successfully created your request!"
            });
        }

19 View Source File : DownloaderObjectModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

public async Task ForceReCheckAsync()
        {
            if (this.IsBeingDownloaded) return;
            await _semapreplacedDownloading.WaitAsync();
            await VerifyDownloadAsync(this.Url);
            _semapreplacedDownloading.Release();
        }

19 View Source File : B2CAuthService.cs
License : MIT License
Project Creator : 1iveowl

private async Task ExecuteSynchronously(Func<Task> task)
        {
            await _semapreplaced.WaitAsync().ConfigureAwait(false);

            try
            {
                await task();
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

19 View Source File : DownloaderViewModel.cs
License : GNU General Public License v3.0
Project Creator : antikmozib

private async Task AddItemsAsync(string destination, bool enqueue, bool start = false, params string[] urls)
        {
            await _semapreplacedUpdatingList.WaitAsync();

            _ctsUpdatingList = new CancellationTokenSource();
            var ct = _ctsUpdatingList.Token;
            RaisePropertyChanged(nameof(this.IsBackgroundWorking));

            int total = urls.Count();
            int maxParallelDownloads = Settings.Default.MaxParallelDownloads;
            var items = new DownloaderObjectModel[urls.Count()];
            var tasks = new List<Task>();
            var forceEnqueue = false;
            var existingUrls = (from di in DownloadItemsList select di.Url).ToArray();
            var existingDestinations = (from di in DownloadItemsList select di.Destination).ToArray();
            List<string> skipping = new List<string>();
            var wasCanceled = false;

            if (start && !enqueue && urls.Count() > Settings.Default.MaxParallelDownloads)
            {
                forceEnqueue = true;
            }

            for (int i = 0; i < total; i++)
            {
                int progress = (int)((double)(i + 1) / total * 100);
                this.Progress = progress;
                this.Status = "Creating download " + (i + 1) + " of " + total + ": " + urls[i];
                RaisePropertyChanged(nameof(this.Status));
                RaisePropertyChanged(nameof(this.Progress));

                if (existingUrls.Contains(urls[i]))
                {
                    skipping.Add(urls[i]);
                    continue;
                }
                var fileName = CommonFunctions.GetFreshFilename(destination + Path.GetFileName(urls[i]));
                if (existingDestinations.Contains(fileName))
                {
                    skipping.Add(urls[i]);
                    continue;
                }

                DownloaderObjectModel item;
                if (forceEnqueue || enqueue)
                {
                    item = new DownloaderObjectModel(
                        ref _client,
                        urls[i],
                        fileName,
                        enqueue: true,
                        Download_Created,
                        Download_Verifying,
                        Download_Verified,
                        Download_Started,
                        Download_Stopped,
                        Download_Enqueued,
                        Download_Dequeued,
                        Download_Finished,
                        Download_PropertyChanged,
                        ProgressReporter,
                        ref _requestThrottler);
                }
                else
                {
                    item = new DownloaderObjectModel(
                        ref _client,
                        urls[i],
                        fileName,
                        enqueue: false,
                        Download_Created,
                        Download_Verifying,
                        Download_Verified,
                        Download_Started,
                        Download_Stopped,
                        Download_Enqueued,
                        Download_Dequeued,
                        Download_Finished,
                        Download_PropertyChanged,
                        ProgressReporter,
                        ref _requestThrottler);
                    if (start)
                    {
                        tasks.Add(item.StartAsync());
                    }
                }
                items[i] = item;

                if (ct.IsCancellationRequested)
                {
                    wasCanceled = true;
                    break;
                }
            }

            if (!wasCanceled)
            {
                this.Status = "Listing...";
                RaisePropertyChanged(nameof(this.Status));

                AddObjects(items);
            }

            _ctsUpdatingList = null;

            this.Progress = 0;
            this.Status = "Ready";
            RaisePropertyChanged(nameof(this.Status));
            RaisePropertyChanged(nameof(this.Progress));
            RaisePropertyChanged(nameof(this.IsBackgroundWorking));

            _semapreplacedUpdatingList.Release();

            RefreshCollection();

            if (!wasCanceled)
            {
                if (skipping.Count > 0)
                {
                    _showUrls(
                        skipping, "Duplicate Entries",
                        "The following URLs were not added because they are already in the list:");
                }

                if ((enqueue && start) || forceEnqueue)
                {
                    await QueueProcessor.StartAsync();
                }
                else
                {
                    await Task.WhenAll(tasks);
                }
            }
        }

19 View Source File : AmmoUpgradesControl.xaml.cs
License : MIT License
Project Creator : AkiniKites

public override async Task Initialize()
        {
            await _lock.WaitAsync();
            {
                ResetSelected.Enabled = false;

                IoC.Notif.ShowStatus("Loading upgrades list...");

                Upgrades.Clear();


                var upgrades = (await Logic.GenerateUpgradeList(IoC.Archiver.LoadGameFileAsync)).Values
                    .OrderBy(x => x.Level).ToList();
                Upgrades.AddRange(upgrades);

                await UpdateDisplayNames(Upgrades);

                RefreshView();
            }
            _lock.Release();
        }

19 View Source File : WebSocketClient.cs
License : MIT License
Project Creator : Aiko-IT-Systems

public async Task SendMessageAsync(string message)
        {
            if (this._ws == null)
                return;

            if (this._ws.State != WebSocketState.Open && this._ws.State != WebSocketState.CloseReceived)
                return;

            var bytes = Utilities.UTF8.GetBytes(message);
            await this._senderLock.WaitAsync().ConfigureAwait(false);
            try
            {
                var len = bytes.Length;
                var segCount = len / OutgoingChunkSize;
                if (len % OutgoingChunkSize != 0)
                    segCount++;

                for (var i = 0; i < segCount; i++)
                {
                    var segStart = OutgoingChunkSize * i;
                    var segLen = Math.Min(OutgoingChunkSize, len - segStart);

                    await this._ws.SendAsync(new ArraySegment<byte>(bytes, segStart, segLen), WebSocketMessageType.Text, i == segCount - 1, CancellationToken.None).ConfigureAwait(false);
                }
            }
            finally
            {
                this._senderLock.Release();
            }
        }

19 View Source File : AsyncLock.cs
License : MIT License
Project Creator : 1100100

internal async Task ObtainLockAsync()
            {
                while (!TryEnter())
                {
                    //we need to wait for someone to leave the lock before trying again
                    await _parent._retry.WaitAsync();
                }
            }

19 View Source File : B2CAuthService.cs
License : MIT License
Project Creator : 1iveowl

private async Task<T> ExecuteSynchronously<T>(Func<Task<T>> task)
        {
            await _semapreplaced.WaitAsync().ConfigureAwait(false);

            try
            {
                return await task();
            }
            finally
            {
                _semapreplaced.Release();
            }

        }

19 View Source File : AsyncLock.cs
License : MIT License
Project Creator : actions

public Task<IDisposable> LockAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            // Don't preplaced cancellationToken to the semapreplaced. If we can't acquire the semapreplaced immediately
            // we'll still get the waitTask returned immediately (with IsCompleted = false)
            // and then we'll end up in the else block where we add a continuation to the waitTask which will honor the cancellationToken
            Task waitTask = m_semapreplaced.WaitAsync();

            if (waitTask.IsCompleted)
            {
                return m_releaser;
            }
            else
            {
                return waitTask.ContinueWith(
                    (task, state) => (IDisposable)state,
                    m_releaser.Result,
                    cancellationToken,
                    TaskContinuationOptions.ExecuteSynchronously,
                    TaskScheduler.Default);
            }
        }

19 View Source File : Remote.cs
License : MIT License
Project Creator : AiursoftWeb

public async Task PullAsync()
        {
            if (ContextRepository == null)
            {
                throw new ArgumentNullException(nameof(ContextRepository), "Please add this remote to a repository.");
            }
            await PullLock.WaitAsync();
            var downloadResult = await ConnectionProvider.Download(PullPointer?.Id);
            if (downloadResult.Any())
            {
                ContextRepository.OnPulled(downloadResult, this);
            }
            PullLock.Release();
        }

19 View Source File : Program.cs
License : MIT License
Project Creator : allantargino

private async static void ProcessJob()
        {
            _lastExecution = DateTime.Now;
            await _semapreplaced.WaitAsync();

            try
            {
                _message = GetMessage();

                if (_message == null)
                    return;

                if (_message.DequeueCount > MAX_DEQUEUE_COUNT)
                {
                    UnQueueMessageAsync(_message);
                    return;
                }

                var pdfFileUrl = _message.replacedtring;

                Console.WriteLine($"Downloading resource - {pdfFileUrl}");

                var pdfTempFile = string.Empty;

                try
                {
                    pdfTempFile = DownloadTempPdf(pdfFileUrl, _tempFolder);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error: {ex.Message}");
                    Console.WriteLine($"Error: {ex.InnerException}");
                }

                if (!String.IsNullOrWhiteSpace(pdfTempFile))
                {
                    Stream[] pdfPageImageList = null;

                    using (var pdfInput = File.OpenRead(pdfTempFile))
                    {
                        Console.WriteLine("Generating Image stream array");
                        PdfImageConverter imageConverter = new PdfImageConverter(_gs, _tempFolder, _resolution);

                        try
                        {
                            //The array of streams will respect the page number-1, page 1 equal index 0;
                            imageConverter.GenerateImage(pdfInput, ref pdfPageImageList);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Error generating pdf images {ex.Message}");
                        }
                    }

                    if (pdfPageImageList == null)
                        Console.WriteLine($"No Pages was generated!");
                    else
                    {
                        Console.WriteLine($"Uploading {pdfPageImageList.Length} Images to final Storage Account");
                        FileInfo info = new FileInfo(pdfTempFile);

                        try
                        {
                            UploadImages(pdfPageImageList, info.Name.ToUpper().Replace(".PDF", ""));
                            await _queue.DeleteMessageAsync(_message);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine($"Error Uploading Images: {ex.Message}");
                        }
                    }

                    try
                    {
                        File.Delete(pdfTempFile);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Error trying to delete the temp file {pdfTempFile}: {ex.Message}");
                    }
                }
                else
                {
                    Console.WriteLine($"PDF {pdfFileUrl} is being process!");
                }
            }
            finally
            {
                _semapreplaced.Release();
            }
        }

See More Examples