I have a multi-threaded application and i'm using this to control the no.of processes (2). I want to process files only for specified time duration. Below is my approach. I'm getting The CancellationTokenSource has been disposed. error.
If i'm not dispoing the cts.Dispose(); then the process is not stooping after 10 sec. It is keep on processing till 1000. Can any one help me here.
Note: I've a 1000 files. Requirement is process files with in a given time (10 sec) by controlling the number of process (2) and sleep in between (some x ms).
Below is my code
class Program
{
static void Main(string[] args)
{
try
{
LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(2);
List<Task> tasks = new List<Task>();
TaskFactory factory = new TaskFactory(lcts);
CancellationTokenSource cts = new CancellationTokenSource();
for (int i = 0; i < 1000; i++)
{
int i1 = i;
var t = factory.StartNew(() =>
{
if (cts != null)
Console.WriteLine("{0} --- {1}", i1, GetGuid(cts.Token));
}, cts.Token);
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray(), 10000, cts.Token);
cts.Dispose();
Console.WriteLine("\n\nSuccessful completion.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static Guid GetGuid(CancellationToken cancelToken)
{
if (cancelToken.IsCancellationRequested)
{
return Guid.Empty;
}
Thread.Sleep(TimeSpan.FromSeconds(1));
return Guid.NewGuid();
}
}
What you can do is you can run a Task that will change your Cancellation Token state to canceled after some time.
Like this :
class Program
{
public static void ProcessFiles(CancellationToken cts)
{
try
{
LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(2);
List<Task> tasks = new List<Task>();
TaskFactory factory = new TaskFactory(lcts);
for (int i = 0; i < 1000; i++)
{
int i1 = i;
var t = factory.StartNew(() =>
{
if (cts != null) Console.WriteLine("{0} --- {1}", i1, GetGuid());
}, cts);
tasks.Add(t);
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine("\n\nSuccessful completion.");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
Task.Factory.StartNew(() => { Thread.Sleep(10000); cts.Cancel(); });
ProcessFiles(cts.Token);
Console.ReadKey();
}
private static Guid GetGuid()
{
Thread.Sleep(TimeSpan.FromSeconds(1));
return Guid.NewGuid();
}
}
Related
I am creating console app to simulate server. I create multiple virus files together using multiple threads to see whether all files get quarantined, if yes, how long it takes to quarantined. The problem with multithreading application is one thread starts writing another thread so I get exception - The process can not access the file X because the file is being used by another process. This is the reason that all files don't get quarantined. I use framework 4.5.2
I have created app using thread and task. I don't get the desire result. What is the best practice to write this app? Thank you for helping me in advance.
Using Thread:
class Program
{
static string folderPath;
static readonly string fileContent = #"X5O!P%#AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
static void Main(string[] args)
{
folderPath = "F:\VirusScan";
int counter = 1000;
for (int i = 0; i < counter; i++)
{
var thread = new Thread(() => GenerateVirusFile(i));
thread.Start();
}
Console.ReadKey();
}
static void GenerateVirusFile(int i)
{
string filePath = $#"{folderPath}\TestForVirusScan_{i}_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(fileContent);
}
var timer = Stopwatch.StartNew();
while (true)
{
if (!File.Exists(filePath))
{
Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
break;
}
else
{
Thread.Sleep(1);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
}
}
}
Using Task:
class Program
{
static string folderPath;
static readonly string fileContent = #"X5O!P%#AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
static void Main(string[] args)
{
folderPath = "F:\VirusScan";
int counter = 1000;
List<Task> tasks = new List<Task>();
for (int i = 1; i <= counter; i++)
{
Task newTask = new Task((x) => GenerateVirusFile(x), i);
tasks.Add(newTask);
}
foreach (var task in tasks)
{
task.Start();
}
Task.WaitAll(tasks.ToArray());
Console.ReadKey();
}
public static void GenerateVirusFile(object i)
{
string filePath = $#"{folderPath}\TestForVirusScan_{i}_{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.txt";
try
{
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine(fileContent);
}
var timer = Stopwatch.StartNew();
while (true)
{
if (!File.Exists(filePath))
{
Console.WriteLine($"{i}: File was removed in {timer.ElapsedMilliseconds}ms");
break;
}
else
{
Thread.Sleep(1);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"{i}: Exception {ex.GetType().Name} occurred: {ex.Message}");
}
}
}
The problem is in the following code:
for (int i = 0; i < counter; i++)
{
var thread = new Thread(() => GenerateVirusFile(i));
thread.Start();
}
The closure () => GenerateVirusFile(i) is referencing changing variable
Rewrite it in the following way:
Parallel.For(0, counter, GenerateVirusFile);
Have you tried something like this in your loop:
int x = i;
var thread = new Thread(() => GenerateVirusFile(x));
this prevents that the same i is used for more threads/file names.
If have 2 methods I want to run in parallel on different threads, let's call them Task1 and Task2. If Task2 completes before Task1 then I want the results of both combined into a list. If Task1 completes and Task2 is still running, it should cancel Task2 to avoid further processing. WhenAll doesn't work because it will wait for both tasks. WhenAny doesn't work because it will return if either finish first.
This is the solution I came up with.
static void Main(string[] args)
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
var rates = new List<string>();
try
{
var p44Task = Task.Factory.StartNew(() => GetP44Rates(token), token);
var mgRates = await GetMGRates();
rates.AddRange(mgRates);
if (p44Task.IsCompleted && p44Task.Result.IsCompleted)
{
rates.AddRange(p44Task.Result.Result);
}
else
{
// Cancel the p44 task
tokenSource.Cancel();
}
}
catch (Exception e)
{
tokenSource.Cancel();
Console.WriteLine(e);
}
foreach (var rate in rates)
{
Console.WriteLine(rate);
}
Console.ReadLine();
}
private static async Task<List<string>> GetMGRates(CancellationToken cancellationToken)
{
var rates = new List<string>();
for (var i = 0; i < 10; i++)
{
rates.Add($"MG: {i}");
Console.WriteLine($"MG inside {i}");
await Task.Delay(1000);
}
return rates;
}
private static async Task<List<string>> GetP44Rates(CancellationToken ct)
{
var rates = new List<string>();
for (var i = 0; i < 50; i++)
{
rates.Add($"P44: {i}");
Console.WriteLine($"P44: {i}");
await Task.Delay(1000);
if (ct.IsCancellationRequested)
{
Console.WriteLine("bye from p44.");
Console.WriteLine("\nPress Enter to quit.");
ct.ThrowIfCancellationRequested();
}
}
return rates;
}
I have a very very strange problem, and here's my codes now:
namespace TaskParallelTest
{
using System.Threading;
using System.Threading.Tasks;
using System;
using System.IO;
public class Program
{
static Program()
{
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
}
private static void DoPrint(int id, CancellationToken cToken)
{
Thread.Sleep(100);
if (!cToken.IsCancellationRequested)
{
Console.WriteLine("Id is:" + id + ";Current State:" + cToken.IsCancellationRequested);
cToken.Register(() => Console.WriteLine("Rollback for:" + id));
}
}
static void Main(string[] args)
{
CancellationTokenSource cTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
for (int i = 1; i < 6; i++)
{
cTokenSource.Token.ThrowIfCancellationRequested();
DoPrint(i, cTokenSource.Token);
}
}, cTokenSource.Token);
Random r = new Random();
Thread.Sleep(400);
cTokenSource.Cancel(true);
Thread.Sleep(10000);
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("OK");
Console.ReadLine();
}
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
File.WriteAllText("C:\\Resume\\Error.txt", e.Exception.StackTrace);
e.SetObserved();
}
}
}
What makes me feel mad is why the event of "UnobservedTaskException" cannot be caught? I used GC.Collect() and Thread.Sleep(), but without any help……?
Sometimes, the "Error.txt" wasn't created, and sometimes, the file created without anything there....?
【Solved——Now acording to the suggestions, here's the answer】
1) Notice that I should remove "Cancellation" and mock an exception here:
static void Main(string[] args)
{
CancellationTokenSource cTokenSource = new CancellationTokenSource();
Task.Run(() =>
{
for (int i = 1; i < 6; i++)
{
if (i==5)
{
throw new Exception("Error occured!");
}
DoPrint(i, cTokenSource.Token);
}
},cTokenSource.Token)
.ContinueWith
(
t =>
{
Console.WriteLine("Error has happened now.");
Console.WriteLine(t.IsFaulted);
},
TaskContinuationOptions.OnlyOnFaulted
);
Thread.Sleep(400);
//cTokenSource.Cancel();
//Thread.Sleep(2000);
GC.Collect();
GC.WaitForPendingFinalizers();
//Thread.Sleep(6000);
Console.WriteLine("OK");
Console.ReadLine();
}
2) Then flatten the Exception (because's that an aggregative exception):
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
foreach (var item in e.Exception.Flatten().InnerExceptions)
{
Console.WriteLine(item.StackTrace);
}
e.SetObserved();
}
If OperationCanceledException is thrown for particular token, and this is the same token you passed when creating the task - it is not treated as unhandled\unobserved exception, because it's just normal, expected cancellation flow. This exception will instead just set task state to Cancelled. That is so in your case too:
var task = Task.Run(() =>
{
for (int i = 1; i < 6; i++)
{
// this exception is associated with cTokenSource.Token
cTokenSource.Token.ThrowIfCancellationRequested();
DoPrint(i, cTokenSource.Token);
}
}, cTokenSource.Token); // and this is the same token you pass when creating a task
If that were not the case (for example, you pass different token when creating a task) - exception will be intercepted by UnobservedTaskException handler.
Question is: why at all you want this exception to be treated as unobserved? You expected the task can be cancelled, you then cancel it, it's now in Cancelled state. Nothing unobserved\unhandled.
This is my function that received List of files and do my stuff:
private IEnumerable<string> _source;
public void doWork()
{
ParallelLoopState pls = new ParallelLoopState();
pls.LowestBreakIteration = 5;
_tokenSource = new CancellationTokenSource();
var token = _tokenSource.Token;
Task.Factory.StartNew(() =>
{
try
{
Parallel.ForEach(_source,
new ParallelOptions
{
MaxDegreeOfParallelism = _parallelThreads //limit number of parallel threads
},
file =>
{
if (token.IsCancellationRequested)
return;
//do work...
});
}
catch (Exception)
{ }
}, _tokenSource.Token).ContinueWith(
t =>
{
//finish...
}
, TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
);
}
I saw that this ForEach can receive ParallelOptions add i wonder how to use it in order to run this operation many times.
As you can see in this code:
public async void TaskDelayTest()
{
while (LoopCheck)
{
for (int i = 0; i < 100; i++)
{
textBox1.Text = i.ToString();
await Task.Delay(1000);
}
}
}
I want it to set textbox to string value of i with one second period until I set LoopCheck value to false . But what it does is that it creates all iteration ones in for all and even if I set LoopCheck value to false it still does what it does asyncronously.
I want to cancel all awaited Task.Delay() iteration when I set LoopCheck=false. How can I cancel it?
Use the overload of Task.Delay which accepts a CancellationToken
public async Task TaskDelayTest(CancellationToken token)
{
while (LoopCheck)
{
token.throwIfCancellationRequested();
for (int i = 0; i < 100; i++)
{
textBox1.Text = i.ToString();
await Task.Delay(1000, token);
}
}
}
var tokenSource = new CancellationTokenSource();
TaskDelayTest(tokenSource.Token);
...
tokenSource.Cancel();
If you're going to poll, poll on a CancellationToken:
public async Task TaskDelayTestAsync(CancellationToken token)
{
for (int i = 0; i < 100; i++)
{
textBox1.Text = i.ToString();
await Task.Delay(TimeSpan.FromSeconds(1), token);
}
}
For more information, see the cancellation documentation.
Just a slight comment about having a cancellation token, and using a try-catch to stop it throwing an exception - your iteration block might fail due to a different reason, or it might fail due to a different task getting cancelled (e.g. from an http request timing out in a sub method), so to have the cancellation token not throw an exception you might want a bit more complicated catch block
public async void TaskDelayTest(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
for (int i = 0; i < 100; i++)
{
try
{
textBox1.Text = i.ToString();
await DoSomethingThatMightFail();
await Task.Delay(1000, token);
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
//task is cancelled, return or do something else
return;
}
catch(Exception ex)
{
//this is an actual error, log/throw/dostuff here
}
}
}
}
After running into this problem I wrote a drop in replacement that behaves as expected if you want to do polling:
public static class TaskDelaySafe
{
public static async Task Delay(int millisecondsDelay, CancellationToken cancellationToken)
{
await Task.Delay(TimeSpan.FromMilliseconds(millisecondsDelay), cancellationToken);
}
public static async Task Delay(TimeSpan delay, CancellationToken cancellationToken)
{
var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var task = new TaskCompletionSource<int>();
tokenSource.Token.Register(() => task.SetResult(0));
await Task.WhenAny(
Task.Delay(delay, CancellationToken.None),
task.Task);
}
}
It uses a cancellation token callback to complete a task and then awaits either that synthetic task or the normal Task.Delay with no cancellation token. This way it won't throw an exception when the source token is cancelled, but still responds to the cancellation by returning execution. You still need to check the IsCancellationRequested after calling it to decide what to do if it is cancelled.
Unit tests, if anyone is interested:
[Test]
public async Task TaskDelay_WaitAlongTime()
{
var sw = System.Diagnostics.Stopwatch.StartNew();
await Base.Framework.TaskDelaySafe.Delay(System.TimeSpan.FromSeconds(5), System.Threading.CancellationToken.None);
Assert.IsTrue(sw.Elapsed > System.TimeSpan.FromSeconds(4));
}
[Test]
public async Task TaskDelay_DoesNotWaitAlongTime()
{
var tokenSource = new System.Threading.CancellationTokenSource(250);
var sw = System.Diagnostics.Stopwatch.StartNew();
await Base.Framework.TaskDelaySafe.Delay(System.TimeSpan.FromSeconds(5), tokenSource.Token);
Assert.IsTrue(sw.Elapsed < System.TimeSpan.FromSeconds(1));
}
[Test]
public async Task TaskDelay_PrecancelledToken()
{
var tokenSource = new System.Threading.CancellationTokenSource();
tokenSource.Cancel();
var sw = System.Diagnostics.Stopwatch.StartNew();
await Base.Framework.TaskDelaySafe.Delay(System.TimeSpan.FromSeconds(5), tokenSource.Token);
Assert.IsTrue(sw.Elapsed < System.TimeSpan.FromSeconds(1));
}
using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static DateTime start;
static CancellationTokenSource tokenSource;
static void Main(string[] args)
{
start = DateTime.Now;
Console.WriteLine(start);
TaskDelayTest();
TaskCancel();
Console.ReadKey();
}
public static async void TaskCancel()
{
await Task.Delay(3000);
tokenSource?.Cancel();
DateTime end = DateTime.Now;
Console.WriteLine(end);
Console.WriteLine((end - start).TotalMilliseconds);
}
public static async void TaskDelayTest()
{
tokenSource = new CancellationTokenSource();
try
{
await Task.Delay(2000, tokenSource.Token);
DateTime end = DateTime.Now;
Console.WriteLine(end);
Console.WriteLine((end - start).TotalMilliseconds);
}
catch (TaskCanceledException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
tokenSource.Dispose();
tokenSource = null;
}
}
}
}