Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //IDBConnectInstance.cs
- class IDBConnectInstance
- {
- public static int count;
- public static int SelectRows()
- {
- count = 0;
- for (int i = 0; i < 1000; i++)
- {
- count += i;
- Thread.Sleep(100);
- }
- return count;
- }
- }
- //Form1.cs
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- CancellationTokenSource _tokenSource;
- Task task;
- private void button1_Click(object sender, EventArgs e)
- {
- _tokenSource = new CancellationTokenSource();
- var rows = 0;
- task = new Task(() =>
- {
- rows = IDBConnectInstance.SelectRows();
- }, _tokenSource.Token);
- task.ContinueWith(t =>
- {
- textBox1.Text += "Значение rows:" + rows + Environment.NewLine;
- });
- task.Start();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- _tokenSource.Cancel();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- textBox1.Text += String.Format("Task status:{0}; Counter: {1}{2}", task.Status, IDBConnectInstance.count, Environment.NewLine);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement