M
Markus Freitag
Guest
Hello,
First, why is canceled not worked, not true?
Cancel work, but the variable is wrong state.
Second,
To understand everything better, I'm looking for a
comparison between task, thread, TaskFactory and 'Cancel', which is important.
Console
--------
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleAppTask
{
class Program
{
static void Main(string[] args)
{
TaskExample obj = new TaskExample();
obj.DoIt();
}
public class TaskExample
{
static CancellationTokenSource CTS;
static CancellationToken Token;
public TaskExample()
{
}
protected void Workingloop()
{
for (int i = 0; i < 100; ++i)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
if (Token.IsCancellationRequested)
Token.ThrowIfCancellationRequested();
}
}
public void DoIt()
{
Action<object> action = (object obj) =>
{
Workingloop();
};
CTS = new CancellationTokenSource();
Token = CTS.Token;
Task t = new Task(action, Token);
//Task t = new Task(() =>
//{
// for (int i = 0; i < 100; ++i)
// {
// Console.WriteLine(i.ToString());
// Thread.Sleep(1000);
// if (token.IsCancellationRequested)
// token.ThrowIfCancellationRequested();
// }
//}, token);
t.Start();
Console.WriteLine("Press Enter to cancel operation");
Console.ReadLine();
Console.WriteLine("Cancelling task...");
CTS.Cancel();
//>> The answer is "Task is canceled: FALSE". without ContinueWith!!!!!!!
//This is because the call to WriteLine($"Task is canceled: {t.IsCanceled}") is executed before
//the task has been canceled, i.e.before a OperationCanceledException is thrown from the Task.
//I need to wait until the Task has actually finished executing before I can check its
//status. I can do this using the ContinueWith method:
// --> is clear!!
// --> but is not work, why? ;-)
Console.WriteLine($"AAAA --- Task is canceled: {t.IsCanceled}");
t.ContinueWith(task => Console.WriteLine($"BBBB --- Task is canceled: {t.IsCanceled}"));
Console.ReadLine();
}
}
}
}
Press Enter to cancel operation
0
1
2
3
4
Cancelling task...
AAAA --- Task is canceled: False
BBBB --- Task is canceled: False
Wfp - App with Start and Cancel button
TaskExample TestTaskThreadFactory;
private void StartThread(object sender, RoutedEventArgs e)
{
TestTaskThreadFactory.DoIt();
}
private void CancelThread(object sender, RoutedEventArgs e)
{
TestTaskThreadFactory.CancelTrigger();
}
////
public void CancelTrigger()
{
CTS.Cancel(); //Is the Cancel trigger here right?
}
// Project
using System;
using System.Threading;
using System.Threading.Tasks;
namespace WpfAppDispatcher.Core
{
public class TaskExample
{
static CancellationTokenSource CTS;
static CancellationToken Token;
public TaskExample()
{
}
protected void Workingloop()
{
for (int i = 0; i < 100; ++i)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
if (Token.IsCancellationRequested)
Token.ThrowIfCancellationRequested();
}
}
public void CancelTrigger()
{
CTS.Cancel();
}
public void DoIt()
{
Action<object> action = (object obj) =>
{
Workingloop();
};
CTS = new CancellationTokenSource();
Token = CTS.Token;
Task t = new Task(action, Token);
t.Start();
Console.WriteLine("Press Enter to cancel operation");
Console.ReadLine();
Console.WriteLine("Cancelling task...");
//>> The answer is "Task is canceled: FALSE". without ContinueWith!!!!!!!
//This is because the call to WriteLine($"Task is canceled: {t.IsCanceled}") is executed before
//the task has been canceled, i.e.before a OperationCanceledException is thrown from the Task.
//I need to wait until the Task has actually finished executing before I can check its
//status. I can do this using the ContinueWith method:
// --> is clear!!
// --> but is not work, why? ;-)
Console.WriteLine($"AAAA --- Task is canceled: {t.IsCanceled}");
t.ContinueWith(task => Console.WriteLine($"BBBB --- Task is canceled: {t.IsCanceled}"));
Console.ReadLine();
}
}
}
Many greetings Markus
Continue reading...
First, why is canceled not worked, not true?
Cancel work, but the variable is wrong state.
Second,
To understand everything better, I'm looking for a
comparison between task, thread, TaskFactory and 'Cancel', which is important.
Console
--------
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleAppTask
{
class Program
{
static void Main(string[] args)
{
TaskExample obj = new TaskExample();
obj.DoIt();
}
public class TaskExample
{
static CancellationTokenSource CTS;
static CancellationToken Token;
public TaskExample()
{
}
protected void Workingloop()
{
for (int i = 0; i < 100; ++i)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
if (Token.IsCancellationRequested)
Token.ThrowIfCancellationRequested();
}
}
public void DoIt()
{
Action<object> action = (object obj) =>
{
Workingloop();
};
CTS = new CancellationTokenSource();
Token = CTS.Token;
Task t = new Task(action, Token);
//Task t = new Task(() =>
//{
// for (int i = 0; i < 100; ++i)
// {
// Console.WriteLine(i.ToString());
// Thread.Sleep(1000);
// if (token.IsCancellationRequested)
// token.ThrowIfCancellationRequested();
// }
//}, token);
t.Start();
Console.WriteLine("Press Enter to cancel operation");
Console.ReadLine();
Console.WriteLine("Cancelling task...");
CTS.Cancel();
//>> The answer is "Task is canceled: FALSE". without ContinueWith!!!!!!!
//This is because the call to WriteLine($"Task is canceled: {t.IsCanceled}") is executed before
//the task has been canceled, i.e.before a OperationCanceledException is thrown from the Task.
//I need to wait until the Task has actually finished executing before I can check its
//status. I can do this using the ContinueWith method:
// --> is clear!!
// --> but is not work, why? ;-)
Console.WriteLine($"AAAA --- Task is canceled: {t.IsCanceled}");
t.ContinueWith(task => Console.WriteLine($"BBBB --- Task is canceled: {t.IsCanceled}"));
Console.ReadLine();
}
}
}
}
Press Enter to cancel operation
0
1
2
3
4
Cancelling task...
AAAA --- Task is canceled: False
BBBB --- Task is canceled: False
Wfp - App with Start and Cancel button
TaskExample TestTaskThreadFactory;
private void StartThread(object sender, RoutedEventArgs e)
{
TestTaskThreadFactory.DoIt();
}
private void CancelThread(object sender, RoutedEventArgs e)
{
TestTaskThreadFactory.CancelTrigger();
}
////
public void CancelTrigger()
{
CTS.Cancel(); //Is the Cancel trigger here right?
}
// Project
using System;
using System.Threading;
using System.Threading.Tasks;
namespace WpfAppDispatcher.Core
{
public class TaskExample
{
static CancellationTokenSource CTS;
static CancellationToken Token;
public TaskExample()
{
}
protected void Workingloop()
{
for (int i = 0; i < 100; ++i)
{
Console.WriteLine(i.ToString());
Thread.Sleep(1000);
if (Token.IsCancellationRequested)
Token.ThrowIfCancellationRequested();
}
}
public void CancelTrigger()
{
CTS.Cancel();
}
public void DoIt()
{
Action<object> action = (object obj) =>
{
Workingloop();
};
CTS = new CancellationTokenSource();
Token = CTS.Token;
Task t = new Task(action, Token);
t.Start();
Console.WriteLine("Press Enter to cancel operation");
Console.ReadLine();
Console.WriteLine("Cancelling task...");
//>> The answer is "Task is canceled: FALSE". without ContinueWith!!!!!!!
//This is because the call to WriteLine($"Task is canceled: {t.IsCanceled}") is executed before
//the task has been canceled, i.e.before a OperationCanceledException is thrown from the Task.
//I need to wait until the Task has actually finished executing before I can check its
//status. I can do this using the ContinueWith method:
// --> is clear!!
// --> but is not work, why? ;-)
Console.WriteLine($"AAAA --- Task is canceled: {t.IsCanceled}");
t.ContinueWith(task => Console.WriteLine($"BBBB --- Task is canceled: {t.IsCanceled}"));
Console.ReadLine();
}
}
}
Many greetings Markus
Continue reading...