I am trying to use the NET 4.0 parallel task library to handle multiple FTS queries. If the query takes too much time, I want to cancel it and continue forward with processing the rest.
This code doesn't stop when one query goes over the threshold. I think I'm calling it such that the cancel task and time limit is reached for the whole of the process rather than the single transaction. If I set the time period to be very small (300ms), then it gets called for all search strings.
I think I'm missing something obvious .. thanks in advance for any insight.
Additionally, this still doesn't seem to stop the very long query from executing. Is this even the correct way to cancel a long running query once it's been triggered?
Modified code:
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
var query = searchString.Values.Select(c =>myLongQuery(c)).AsParallel().AsOrdered()
.Skip(counter * numToProcess).Take(numToProcess).WithCancellation(cts.Token);
new Thread(() =>
{
Thread.Sleep(5000);
cts.Cancel();
}).Start();
try
{
List<List<Threads>> results = query.ToList();
foreach (List<Threads> threads in results)
{
// does something with data
}
} catch (OperationCanceledException) {
Console.WriteLine("query took too long");
}