//Методы для возврата в основной поток private void CallBackEratosphene(IAsyncResult ar) { AsyncEratosphene eratospheneDelegate = (AsyncEratosphene)ar.AsyncState; string str = String.Format("Сумма чисел равна: {0}", string.Join(",", eratospheneDelegate.EndInvoke(ar))); this.eratospheneResult = str; MethodInvoker mi = new MethodInvoker(this.UpdateUI); this.BeginInvoke(mi); }
//Запуск расчета алгоритмов private void button1_Click(object sender, EventArgs e) { int max; try { max = (int)numericUpDown1.Value; } catch (Exception) { MessageBox.Show("Введен неверный тип данных. Введите число!"); numericUpDown1.Value = 0; return; } AsyncEratosphene eratospheneDelegate = new AsyncEratosphene(Eratosphene); AsyncCallback cb1 = new AsyncCallback(CallBackEratosphene); IAsyncResult asyncResult = eratospheneDelegate.BeginInvoke(max, cb1, eratospheneDelegate); }