Backgorund worker и потоки - C#
Формулировка задачи:
в общем есть backgroundowrket где вызывается класс где вызывается функция , как видете в ней создаются потоки ! Вопрос как оследить пока каждый из потоков закончится ? чтобы ЛИШЬ ЗАТЕМ закрыть background worker!
Листинг программы
- private void DownloadPageAsync()
- {
- string[] lines = null;
- form1.Invoke(new System.Windows.Forms.MethodInvoker(()=> lines = form1.richTextBox_Dork.Lines));
- ProxySet();
- foreach (var dork in lines)
- {
- if (dork == "")
- {
- continue;
- }
- System.Threading.Thread thrd = new System.Threading.Thread(startCircyle);
- thrd.IsBackground = true;
- thrd.Start(dork);
- }
- int c = 0;
- }
Решение задачи: «Backgorund worker и потоки»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApplication5 {
- class Program {
- static void Main(string[] args) {
- Task[] tasks = new Task[10];
- for (int i = 0; i < tasks.Length; i++) {
- tasks[i] = new Task(() => Run());
- }
- foreach (Task t in tasks) {
- t.Start();
- }
- Task.WaitAll(tasks);
- Console.WriteLine("All tasks completed!");
- Console.ReadLine();
- }
- static void Run() {
- Thread.Sleep(500);
- Console.WriteLine("Id of that thread is: {0}", Thread.CurrentThread.ManagedThreadId);
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д