С#.Rihter.ParallelLINQ
simple example
int[] nums = Enumerable.Range(1, 10).ToArray(); var res = nums.AsParallel().Where(n => n < 5).ToArray(); Thread.Sleep(2000); Console.WriteLine(String.Join(» «, res));
output
1 2 3 4
C#.Rihter.Parallel For, ForEach, Invoke
tasks that can be done in parallel
static void Main(string[] args) { Parallel.For(1, 10, i => DoWork(i)); Thread.Sleep(12000); } static void DoWork(int i) {
C#.Rihter.TaskStatus
static void Main(string[] args) { Task<int> t = new Task<int>(() => { Console.WriteLine(«smth in thread»); return 123; }); Console.WriteLine(t.Status); t.Start(); Console.WriteLine(t.Status); Thread.Sleep(3000); Console.WriteLine(t.Status); }
C#.Rihter.ParentTasks
Example
static void Main(string[] args) { Task<Int32[]> parent = new Task<Int32[]>(() => { var results = new Int32[3]; StartNewTask(results, 0, 1); StartNewTask(results, 1, 2);
FireDAC vs UniDAC. Получение значения первичного ключа новой строки
Два года тому назад я писал о получении в программе значения первичного ключа, который сгенерирован СУБД при добавлении новой строки в таблицу. Мои примеры вызова
C#.Rihter.ContinueWith
static void Main(string[] args) { Task<int> t = new Task<int>(Work); t.Start(); Thread.Sleep(3000); // doing smth in main thread t.ContinueWith(t => Console.WriteLine(«job is done «
C#.Rihter.Task
task allows get result
static void Main(string[] args) { Task<int> t = new Task<int>(n => Sum((int) n), 10000); t.Start(); t.Wait(); Console.WriteLine(«The sum is «
C#.Rihter. How to stop thread work ? Cancellation token
how do i exit from thread
static void Main(string[] args) { var tokenSource = new CancellationTokenSource(); ThreadPool.QueueUserWorkItem(o => ThreadWorker(tokenSource.Token, 1000)); Console.WriteLine(«Press <Enter> to cancel
C#. Rihter. Use Thread from the thread pool
static void Main(string[] args) { Console.WriteLine(«this is main thread»); ThreadPool.QueueUserWorkItem(ThreadWorker, 123); Thread.Sleep(3000); } private static void ThreadWorker(object obj) { Thread.Sleep(1000); Console.WriteLine(«hi from thread pool
С#. Foreground and Background threads
Foreground and background threads.
If background thread then application will not wait until it is over, in other case it will.
static void