Different between Task t = Task.Run and Task.Factory.StartNew

  • Thread starter Thread starter Markus Freitag
  • Start date Start date
M

Markus Freitag

Guest
Hello,
Can someone please explain the differences to me?
When do I take what and why?
When do i take await, when not?

Task.Run I need Start. If I use Task.Factory.StartNew is running immediately.
How can I force a break and then continue without restarting?
Thank you for good, short, understandable explanations in advance.
I think I started a good project to see the differences.
You can expand it to see explanations.
If I use thread, how do i make sure it ends cleanly?
Many greetings Markus

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleAppTaskAsyn
{
public class Possibilities
{
public ManualResetEvent CancelThread = new ManualResetEvent(false);


public Possibilities()
{

}

public void Variant_001()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken token = cancellationTokenSource.Token;

Task t = Task.Run(async () =>
{
for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
while (!token.IsCancellationRequested)
{
Console.Write("*");
await Task.Delay(10); // for me looks is here waitung 10ms
}
}
Console.WriteLine();
}
}, token);


Console.WriteLine("Press Enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();

Console.WriteLine("Press Enter to exit");
Console.ReadLine();
}


public void Variant_002_WithoutAsync()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken token = cancellationTokenSource.Token;

Task t = Task.Run(() =>
{
for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
while (!token.IsCancellationRequested)
{
Console.Write("*");
Task.Delay(10); // for me looks is here not waiting 10ms
}
}
Console.WriteLine();
}
}, token);


Console.WriteLine("Press Enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();

Console.WriteLine("Press Enter to exit");
Console.ReadLine();
}



public async void WorkingloopAsync(CancellationToken token)
{
for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
while (!token.IsCancellationRequested)
{
Console.Write("*");
await Task.Delay(10); // for me looks is here waitung 10ms
}
}
Console.WriteLine();
}
}

public void WorkingloopSync(CancellationToken token)
{
for (int y = 0; y < 100; y++)
{
for (int x = 0; x < 100; x++)
{
while (!token.IsCancellationRequested)
{
Console.Write("*");
Task.Delay(9000); // for me looks is here not waitung 10ms
}
}
Console.WriteLine();
}
}

public void Variant_03_WithFunctionName()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken token = cancellationTokenSource.Token;


//Action<object> action = (object obj) =>
//{
// Workingloop(token);
//};

//Action action2 = () =>
//{
// WorkingloopAsync(token);
//};

Action action3 = () =>
{
WorkingloopSync(token);
};

Task t = new Task(action3, token);
t.Start();

Console.WriteLine("Press Enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();

Console.WriteLine("Press Enter to exit");
Console.ReadLine();

}


public void Variant_04_TaskFactory()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken token = cancellationTokenSource.Token;

//Action<object> action = (object obj) =>
//{
// Workingloop(token);
//};

Action action2 = () =>
{
WorkingloopAsync(token);
};

//Action action3 = () =>
//{
// WorkingloopSync(token);
//};

//Task t = Task.Factory.StartNew(action2, token);
Task t = new TaskFactory(token).StartNew(action2); is this the same es before?

Console.WriteLine("Press Enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();

Console.WriteLine("Press Enter to exit");
Console.ReadLine();

}

////////////////////////////////////////// Threading ///////

public void StopIt()
{
CancelThread.Set();
}

public void StartIt()
{
var thread = new Thread(DoSomething);
thread.IsBackground = true;

//Ensure terminate event is reset
CancelThread.Reset();

thread.Start();

///-------
///
//Thread t = new Thread(DoSomething_2);
//t.Start(this);

Thread thread2 = new Thread(() => DoSomething_2(this));
thread2.Start();


}

private void DoSomething()
{
Thread.CurrentThread.Name = "MyThread.DoSomething-" + Thread.CurrentThread.ManagedThreadId;

for (int i = 0; i < 10000; i++)
{
Console.Write("*AAAAAAA*");
Thread.Sleep(100);
if (CancelThread.WaitOne(0))
return;
}
}

private void DoSomething_2(object parameter)
{
Thread.CurrentThread.Name = "MyThread.DoSomething_2-" + Thread.CurrentThread.ManagedThreadId;

Possibilities whatYouWant = parameter as Possibilities;

for (int i = 0; i < 10000; i++)
{
Console.Write("*BBBBBBB*");
Thread.Sleep(100);
if (CancelThread.WaitOne(0))
return;
}
}
}

/// <summary>
/// Thread with event
/// </summary>

class Program
{
static void Main(string[] args)
{
Possibilities test = new Possibilities();

// test.Variant_001();
// test.Variant_002_WithoutAsync();
// test.Variant_03_WithFunctionName();
test.Variant_04_TaskFactory();


Console.WriteLine("####### Now with Thread ####### ");
Console.ReadLine();
Console.WriteLine("####### Press Enter for START ####### ");
Console.ReadLine();
test.StartIt();
Console.WriteLine("####### Thread is started ####### ");

Console.WriteLine("####### Press Enter for STOP ####### ");
Console.ReadLine();
test.StopIt();


Console.WriteLine("####### Thread is stopped ####### ");

Console.ReadLine();
}
}

}

Continue reading...
 
Back
Top