using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Media; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { static bool GoPauseState=true; static bool isRunning = false; Thread calcThread; Thread timeThread; public Form1() { InitializeComponent(); } private void SimulateCalc() { int countTo = progressBar1.Maximum; while(countTo!=0) { countTo--; progressBar1.PerformStep(); Thread.Sleep(1); } timeThread.Abort(); isRunning = false; } private int SetResult() { int pom = 1; int num = Int32.Parse(textBox1.Text); if (num == 0) throw new Exception("Argument is 0"); while (true) { if (num == 1) break; pom *= num; num--; } Silnia.Text = pom.ToString(); return pom; } private void CountTime() { int timer = 0; while(true) { Thread.Sleep(1000); timer++; SinceStart.Text = timer.ToString(); } } private void StartStopButton_Click(object sender, EventArgs e) { if (isRunning) { } else { try { int value = SetResult(); isRunning = true; progressBar1.Maximum = value; progressBar1.Step = 1; progressBar1.Value = 0; calcThread = new Thread(() => SimulateCalc()); calcThread.Start(); timeThread = new Thread(() => CountTime()); timeThread.Start(); } catch(Exception) { } } } private void button2_Click(object sender, EventArgs e) { try { if (GoPauseState) { calcThread.Suspend(); timeThread.Suspend(); button2.Text = "Go"; GoPauseState = false; } else { calcThread.Resume(); timeThread.Resume(); button2.Text = "Pause"; GoPauseState = true; } } catch(Exception) { } } private void Form1_Load(object sender, EventArgs e) { } private void progressBar1_Click(object sender, EventArgs e) { } } }