views:

47

answers:

2

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");
  }   
A: 

This is just a guess: isn't the problem that the query is lazy (as in normal LINQ) and so it isn't executed until later?

svick
Not sure I understand what you mean .. Because results is not typed? Could you clarify ?
sidd.darko
I think that the line `var results = …` won't actually execute the query. It will be executed only when you actually enumerate the results.
svick
+1  A: 

PLINQ will poll the cancellation token after every some number of elements. If the frequency of checks is insufficient for your application, make sure all expensive delegates in the PLINQ query regularly call cts.Token.ThrowIfCancellationRequested().

For more details, see this article: http://blogs.msdn.com/b/pfxteam/archive/2009/06/22/9791840.aspx

Igor ostrovsky