Compare commits

...

2 Commits

Author SHA1 Message Date
test 782a6da74e anonyme methode in ConsoleTask 2019-05-03 15:44:56 +02:00
test e00f16b679 handle dispose in FormTask 2019-05-03 15:36:43 +02:00
3 changed files with 58 additions and 17 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@ -11,21 +12,38 @@ namespace ConsoleAppTasks
{
static void Main(string[] args)
{
//var t = new Task(DoIt);
var t = new Task(DoIt);
t.Start();
int i = 4;
int y = 0;
//anonyme methode
var t = new Task(() =>
{
Thread.Sleep(2000);
Console.WriteLine("2");
y = 2 * i;
});
t.Start();
i = 99;
t.Wait();
Console.WriteLine(y); //198
/*
for (int i = 0; i < 999; i++)
{
Console.WriteLine("-");
Thread.Sleep(200);
}
*/
Console.ReadLine(); //wait for close
}
@ -37,10 +55,7 @@ namespace ConsoleAppTasks
{
Console.WriteLine("+");
Thread.Sleep(500);
}
}
}
}
}

View File

@ -60,6 +60,8 @@
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

View File

@ -19,12 +19,16 @@ namespace WindowsFormsAppTask
InitializeComponent();
}
private Task t;
private CancellationTokenSource cts;
private void Button1_Click(object sender, EventArgs e)
{
var t = new Task(DoIt);
cts = new CancellationTokenSource();
t = new Task(DoIt, cts.Token);
t.Start();
for (int i = 0; i < 999; i++)
for (var i = 0; i < 999; i++)
{
textBox1.Text += "-";
Application.DoEvents(); //update ui elements
@ -37,24 +41,44 @@ namespace WindowsFormsAppTask
{
//Console.WriteLine(".");
for (int i = 0; i < 999; i++)
for (var i = 0; i < 999; i++)
{
if (textBox1.InvokeRequired)
if (cts.IsCancellationRequested)
{
textBox1.Invoke(new MethodInvoker(() => { textBox1.Text += "x"; }));
return;
}
else
try
{
textBox1.Text += "x";
if (textBox1.InvokeRequired)
textBox1.Invoke(new MethodInvoker(() => { textBox1.Text += "x"; }));
else
textBox1.Text += "x";
}
catch (ObjectDisposedException)
{
return;
}
Random random = new Random();
Thread.Sleep(random.Next(100, 500));
Thread.Sleep(200);
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
}
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;
}
}
}
}