Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1.     //IDBConnectInstance.cs
  2.     class IDBConnectInstance
  3.     {
  4.         public static int count;
  5.  
  6.         public static int SelectRows()
  7.         {
  8.             count = 0;
  9.             for (int i = 0; i < 1000; i++)
  10.             {
  11.                 count += i;
  12.                 Thread.Sleep(100);
  13.             }
  14.             return count;
  15.         }
  16.     }
  17.  
  18.     //Form1.cs
  19.     public partial class Form1 : Form
  20.     {
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         CancellationTokenSource _tokenSource;
  27.         Task task;
  28.        
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             _tokenSource = new CancellationTokenSource();
  32.  
  33.             var rows = 0;
  34.             task = new Task(() =>
  35.             {
  36.                 rows = IDBConnectInstance.SelectRows();
  37.             }, _tokenSource.Token);
  38.             task.ContinueWith(t =>
  39.             {
  40.                 textBox1.Text += "Значение rows:" + rows + Environment.NewLine;
  41.             });
  42.  
  43.             task.Start();
  44.         }
  45.  
  46.         private void button2_Click(object sender, EventArgs e)
  47.         {
  48.             _tokenSource.Cancel();
  49.         }
  50.  
  51.         private void button3_Click(object sender, EventArgs e)
  52.         {
  53.             textBox1.Text += String.Format("Task status:{0}; Counter: {1}{2}", task.Status, IDBConnectInstance.count, Environment.NewLine);
  54.         }
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement