public static void Explain(this ThreadLimit runnable, TextWriter writer) { writer.WriteLine(@" - `TaskScheduler.Current` is floated into async continuations with `Task.Factory.StartNew` - `ConfigureAwait(false)` or `TaskCreationOptions.HideScheduler` allows to opt-out - I would quit the project if you forced me to maintain this code ;) - If you think you need a scheduler you are probably doing it wrong ;) "); }
public static void PrintStartNewBefore(this ThreadLimit runnable, Int32 current) { var scheduler = TaskScheduler.Current as LimitedConcurrencyLevelTaskScheduler; if (scheduler != null) { Console.WriteLine($"[Thread{Thread.CurrentThread.ManagedThreadId}](Limit): start Task.Factory.StartNew({current})"); } else { Console.WriteLine($"[Thread{Thread.CurrentThread.ManagedThreadId}](none): start Task.Factory.StartNew({current})"); } }
public static void PrintRunAfter(this ThreadLimit runnable, Int32 current) { var scheduler = TaskScheduler.Current as LimitedConcurrencyLevelTaskScheduler; if (scheduler != null) { Console.WriteLine($"[Thread{Thread.CurrentThread.ManagedThreadId}](Limit): done Task.Run({current})"); } else { Console.WriteLine($"[Thread{Thread.CurrentThread.ManagedThreadId}](none): done Task.Run({current})"); } }
public static async Task PumpWithSemaphoreConcurrencyTwo(this ThreadLimit runnable, Func <Int32, CancellationToken, Task> action) { var maxConcurrency = 2; var semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency); var tokenSource = new CancellationTokenSource(); tokenSource.CancelAfter(TimeSpan.FromSeconds(2)); var token = tokenSource.Token; var pumpTask = Task.Run(async() => { var numberOfTasks = 0; while (!token.IsCancellationRequested) { await semaphore.WaitAsync(token); action(numberOfTasks++, token) .ContinueWith((t, state) => { var s = (SemaphoreSlim)state; s.Release(); }, semaphore, TaskContinuationOptions.ExecuteSynchronously) .Ignore(); } }); async Task WaitForPendingWork() { while (semaphore.CurrentCount != maxConcurrency) { await Task.Delay(50); } } await pumpTask.IgnoreCancellation(); await WaitForPendingWork(); Console.WriteLine("..."); Console.ReadLine(); }
public static void SaveConfig() { /// Create xml config XElement root = new XElement("FileManagerConfig"); /// Records XElement record = new XElement("record"); /// History list XElement history_list = new XElement("history"); foreach (ConnectionRecord c in Histories) { XElement item = new XElement("item"); item.SetElementValue("ip", c.Info); item.SetElementValue("star", c.IsStarred ? "1" : "0"); history_list.Add(item); } record.Add(history_list); root.Add(record); /// Settings XElement settings = new XElement("settings"); settings.SetElementValue("ClickCloseToMinimize", ClickCloseToMinimize.ToString()); settings.SetElementValue("UpdateLengthThreshold", UpdateLengthThreshold.ToString()); settings.SetElementValue("UpdateTimeThreshold", UpdateTimeThreshold.ToString()); settings.SetElementValue("SaveRecordInterval", SaveRecordInterval.ToString()); settings.SetElementValue("ConnectionMonitorRecordCount", ConnectionMonitorRecordCount.ToString()); settings.SetElementValue("ConnectionMonitorRecordInterval", ConnectionMonitorRecordInterval.ToString()); settings.SetElementValue("DefaultPort", DefaultServerPort.ToString()); settings.SetElementValue("ThreadLimit", ThreadLimit.ToString()); settings.SetElementValue("SmallFileLimit", SmallFileThreshold.ToString()); settings.SetElementValue("SocketSendTimeout", SocketSendTimeout.ToString()); settings.SetElementValue("SocketReceiveTimeout", SocketReceiveTimeout.ToString()); root.Add(settings); root.Save(ConfigPath); }
public static void PrintOptions(this ThreadLimit runnable, TaskCreationOptions options, Boolean configureAwait) { Console.WriteLine($"Running with creation options {options} and ConfigureAwait({configureAwait})"); }