SoftwareEngineering2/WindowsFormsAppTask/WindowsFormsAppTask/Form1.cs

84 lines
1.9 KiB
C#
Raw Normal View History

2019-05-03 15:09:18 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsAppTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
2019-05-03 15:36:43 +02:00
private Task t;
private CancellationTokenSource cts;
2019-05-03 15:09:18 +02:00
private void Button1_Click(object sender, EventArgs e)
{
2019-05-03 15:36:43 +02:00
cts = new CancellationTokenSource();
t = new Task(DoIt, cts.Token);
2019-05-03 15:09:18 +02:00
t.Start();
2019-05-03 15:36:43 +02:00
for (var i = 0; i < 999; i++)
2019-05-03 15:09:18 +02:00
{
textBox1.Text += "-";
Application.DoEvents(); //update ui elements
Thread.Sleep(100);
}
}
private void DoIt()
{
//Console.WriteLine(".");
2019-05-03 15:36:43 +02:00
for (var i = 0; i < 999; i++)
2019-05-03 15:09:18 +02:00
{
2019-05-03 15:36:43 +02:00
if (cts.IsCancellationRequested)
{
return;
}
try
2019-05-03 15:09:18 +02:00
{
2019-05-03 15:36:43 +02:00
if (textBox1.InvokeRequired)
textBox1.Invoke(new MethodInvoker(() => { textBox1.Text += "x"; }));
else
textBox1.Text += "x";
2019-05-03 15:09:18 +02:00
}
2019-05-03 15:36:43 +02:00
catch (ObjectDisposedException)
2019-05-03 15:09:18 +02:00
{
2019-05-03 15:36:43 +02:00
return;
2019-05-03 15:09:18 +02:00
}
2019-05-03 15:36:43 +02:00
Thread.Sleep(200);
2019-05-03 15:09:18 +02:00
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
2019-05-03 15:36:43 +02:00
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
cts.Cancel();
for (var i = 0; i < 999; i++)
{
var ii = 0;
}
}
2019-05-03 15:09:18 +02:00
}
}