Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a custom table control where i add for example 1.5 million items.

If i click sort which takes around 15 seconds but exit after 5, it will continue on a separate thread for a further 10 until the sort has completed.

I have used the task and cancellation token technique to set the cancellation tokens cancel to true when the control is disposed. I thought it was possible to add a where clause to the linq expression to exit the thread when the control has been disposed. However it continues to run until the sort is complete opposed to breaking out. Not sure if this is a misunderstanding that the where will in effect break out of the sort loop.

Does anyone know how i could use the cancellation token to correctly exit this linq expression?

VB
m_listViewItems = new ObservableCollection<ListItem>(
                    from i in m_listViewItems
                    where !m_cancelationTokenSource.IsCancellationRequested
                    orderby (primaryTypeIsKnown)
                            ? Convert.ChangeType(i.SubItems[sortIndex], primaryItemType, CultureInfo.InvariantCulture)
                            : i.SubItems[sortIndex].ToString() descending,
                            (secondaryTypeIsKnown)
                            ? Convert.ChangeType(i.SubItems[columnToCompareIndex], secondaryItemType,
                                CultureInfo.InvariantCulture)
                            : i.SubItems[columnToCompareIndex].ToString() descending
                    select i);


Thanks in advance George
Posted

1 solution

'Where' is executed before OrderBy .. so it won't cancel that....

have a look at this one here
http://stackoverflow.com/questions/6700140/proper-way-to-use-linq-with-cancellationtoken[^]
 
Share this answer
 
Comments
gwithey 29-May-15 5:23am    
Thank you F.Xaver i will give this a try
F. Xaver 29-May-15 6:04am    
.TakeWhile(item => !tocken.IsCancellationRequested)
also works fine.. but i don't know how thats written in Query form
gwithey 29-May-15 6:05am    
The following using the solution you linked has reduced my sort time 2 fold and enabled me to cancel with a token...

// Sort by Ascending
m_listViewItems = new ObservableCollection<renishawlistitem>(
this.Items
.AsParallel()
.WithDegreeOfParallelism(2) // Sorts 2 in parellel (Down from 6 seconds to 3 over 1.5 million)
.WithCancellation(m_cancelationTokenSource.Token) // Enables the sort to quit when token is false on dispose etc
.OrderBy(
x => (primaryTypeIsKnown)
? Convert.ChangeType(x.SubItems[sortIndex], primaryItemType,
CultureInfo.InvariantCulture)
: x.SubItems[sortIndex].ToString())
.ThenBy(
x => (secondaryTypeIsKnown)
? Convert.ChangeType(x.SubItems[columnToCompareIndex], secondaryItemType,
CultureInfo.InvariantCulture)
: x.SubItems[columnToCompareIndex].ToString()));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900