Facebook
From Oskar, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 288
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Media;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace WindowsFormsApp1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         static bool GoPauseState=true;
  18.         static bool isRunning = false;
  19.         Thread calcThread;
  20.         Thread timeThread;
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         private void SimulateCalc()
  26.         {
  27.             int countTo = progressBar1.Maximum;
  28.             while(countTo!=0)
  29.             {
  30.                 countTo--;
  31.                 progressBar1.PerformStep();
  32.                 Thread.Sleep(1);
  33.             }
  34.             timeThread.Abort();
  35.             isRunning = false;
  36.            
  37.         }
  38.         private int SetResult()
  39.         {
  40.             int pom = 1;
  41.             int num = Int32.Parse(textBox1.Text);
  42.  
  43.             if (num == 0)
  44.                 throw new Exception("Argument is 0");
  45.  
  46.             while (true)
  47.             {
  48.                 if (num == 1)
  49.                     break;
  50.                 pom *= num;
  51.                 num--;
  52.             }
  53.             Silnia.Text = pom.ToString();
  54.             return pom;
  55.         }
  56.         private void CountTime()
  57.         {
  58.             int timer = 0;
  59.             while(true)
  60.             {
  61.                 Thread.Sleep(1000);
  62.                 timer++;
  63.                 SinceStart.Text = timer.ToString();
  64.             }
  65.         }
  66.         private void StartStopButton_Click(object sender, EventArgs e)
  67.         {
  68.             if (isRunning)
  69.             {
  70.                
  71.             }
  72.             else
  73.             {
  74.                 try
  75.                 {
  76.                     int value = SetResult();
  77.                     isRunning = true;
  78.                     progressBar1.Maximum = value;
  79.                     progressBar1.Step = 1;
  80.                     progressBar1.Value = 0;
  81.  
  82.                     calcThread = new Thread(() => SimulateCalc());
  83.                     calcThread.Start();
  84.  
  85.                     timeThread = new Thread(() => CountTime());
  86.                     timeThread.Start();
  87.                 }
  88.                 catch(Exception)
  89.                 {
  90.  
  91.                 }
  92.             }
  93.            
  94.         }
  95.  
  96.         private void button2_Click(object sender, EventArgs e)
  97.         {
  98.             try
  99.             {
  100.                 if (GoPauseState)
  101.                 {
  102.                     calcThread.Suspend();
  103.                     timeThread.Suspend();
  104.  
  105.                     button2.Text = "Go";
  106.                     GoPauseState = false;
  107.                 }
  108.                 else
  109.                 {
  110.                     calcThread.Resume();
  111.                     timeThread.Resume();
  112.  
  113.                     button2.Text = "Pause";
  114.                     GoPauseState = true;                
  115.                 }
  116.             }
  117.             catch(Exception)
  118.             {
  119.  
  120.             }
  121.  
  122.         }
  123.         private void Form1_Load(object sender, EventArgs e)
  124.         {
  125.         }
  126.         private void progressBar1_Click(object sender, EventArgs e)
  127.         {
  128.            
  129.         }
  130.     }
  131. }
  132.