i have a list of files, every file that i need to run (PCAP file - transmit the packets) has its own running time.
because i want the option to handle more than 1 file parallel i am using this function that get IEnumerable<string> source
and MAX number of parallel threads:
public void doWork(IEnumerable<string> _source, int parallelThreads)
{
_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;
//process my file...
});
}
catch (Exception)
{ }
}, _tokenSource.Token).ContinueWith(
t =>
{
//finish all the list...
}
, TaskScheduler.FromCurrentSynchronizationContext() //to ContinueWith (update UI) from UI thread
);
}
for example if i have list of 10 files and my max number of parallel threads is 4 so my program start to transmit 4 files parallel and after the first file finish another 1 file automatic start and this is works fine if i transmit all my list 1 time.
after added the option to play all the list in loop i have a problem, if i want to play for example all the list twice, after the first loop end the second begin and in this loop after the first file finish all the UI stuck and not respond. i have talk with friend of mind that he is C# developer and he told me that is probably Task known issue that sometimes get into deadlock. is it possible to use another Class instead of Task ?