public ConnectionListener( IConnectionSettings connectionSettings, IConnectionFactory connectionFactory, IPlayerRepository playerRepository, IAdminCredentials adminCredentials) { _connectionSettings = connectionSettings; _connectionFactory = connectionFactory; _playerRepository = playerRepository; _adminCredentials = adminCredentials; _connections = new List<IConnection>(); var taskScheduler = new ThreadPerTaskScheduler(); _taskFactory = new TaskFactory(taskScheduler); }
private static Task ForCountAsync(int count, int dop, Func <int, Task> bodyAsync) { var sched = new ThreadPerTaskScheduler(); int nextAvailableIndex = 0; return(Task.WhenAll(Enumerable.Range(0, dop).Select(_ => Task.Factory.StartNew(async delegate { int index; while ((index = Interlocked.Increment(ref nextAvailableIndex) - 1) < count) { try { await bodyAsync(index); } catch { Volatile.Write(ref nextAvailableIndex, count); // avoid any further iterations throw; } } }, CancellationToken.None, TaskCreationOptions.None, sched).Unwrap()))); }
private static Task ForCountAsync(int count, int dop, Func<int, Task> bodyAsync) { var sched = new ThreadPerTaskScheduler(); int nextAvailableIndex = 0; return Task.WhenAll(Enumerable.Range(0, dop).Select(_ => Task.Factory.StartNew(async delegate { int index; while ((index = Interlocked.Increment(ref nextAvailableIndex) - 1) < count) { try { await bodyAsync(index); } catch { Volatile.Write(ref nextAvailableIndex, count); // avoid any further iterations throw; } } }, CancellationToken.None, TaskCreationOptions.None, sched).Unwrap())); }
private async void startUpload_btn_Click(object sender, EventArgs e) { puaseLoad_btn.Enabled = true; if (allObjects.Count == 0) { MessageBox.Show("Upload at least 1 file"); return; } if (!imgur_checkBox.Checked && !postimages_checkBox.Checked) { MessageBox.Show("Please select at least 1 checkbox", "Ok", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } proc_pb.Visible = true; proc_pb.Maximum = allObjects.Count; var scheduler = new ThreadPerTaskScheduler(); var resu = await Task.Run(async() => { LoaderImage li = new LoaderImage(); List <CastModel> listsCastModels = new List <CastModel>(); var processorCount = Environment.ProcessorCount; var semaphore = new SemaphoreSlim(processorCount, processorCount); var countdown = new CountdownEvent(allObjects.Count); foreach (var record in allObjects) { await semaphore.WaitAsync(); _ = Task.Factory.StartNew(async() => { try { if (record.Field3.Length > 0) { string imgurImg = string.Empty; string postimages = string.Empty; if (imgur_checkBox.Checked) { try { var policy = Policy.Handle <Exception>() .RetryAsync(5); imgurImg = await policy.ExecuteAsync(() => li.LoadImgOnImgur(record.Field3, clientId_txtBox.Text, proxyList)); log_txtBox.Invoke(new Action(() => { log_txtBox.Text += $"Photo {record.Field3} successfully loaded on Imgur\n"; })); } catch (Exception ex) { errorsFiles.Add(record); log_txtBox.Invoke(new Action(() => { log_txtBox.Text += $"Photo {record.Field3} has error {ex}\n"; })); File.AppendAllText("log.txt", ex.ToString()); return; } } if (postimages_checkBox.Checked) { try { var policy = Policy.Handle <Exception>() .RetryAsync(5); postimages = await policy.ExecuteAsync(() => li.LoadImgOnpostimages(record.Field3, clientIdPostImages_txtBox.Text, proxyList)); log_txtBox.Invoke(new Action(() => { log_txtBox.Text += $"Photo {record.Field3} successfully loaded on PostImages\n"; })); } catch (Exception ex) { errorsFiles.Add(record); log_txtBox.Invoke(new Action(() => { log_txtBox.Text += $"Photo {record.Field3} has error {ex}\n"; })); File.AppendAllText("log.txt", ex.ToString()); return; } } var newObj = new CastModel() { Title = record.Title, Field1 = record.Field1, Field2 = record.Field2, Field3 = record.Field3, Field4 = record.Field4, ImgurUrl = imgurImg, PostImagesUrl = postimages }; lock (listsCastModels) listsCastModels.Add(newObj); } proc_pb.Invoke(new Action(() => { proc_pb.PerformStep(); })); _pause.WaitOne(); _pause.Set(); } finally { semaphore.Release(); countdown.Signal(); } }, default, TaskCreationOptions.None, scheduler);