线程结束时退出linq表达式 [英] Exit linq expression when thread ends

查看:79
本文介绍了线程结束时退出linq表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个自定义表格控件,我加上例如150万件物品。



如果我单击排序大约需要15秒但是在5之后退出,它将继续在单独的线程上再继续10,直到排序完成。



我已经使用任务和取消令牌技术将取消控制令牌取消设置为真时取消为真。我认为可以在linq表达式中添加where子句,以便在处理控件时退出线程。然而,它继续运行,直到排序完成而不是爆发。不确定这是否是一个误解,实际上哪里会打破排序循环。



有谁知道如何使用取消令牌来正确退出这个linq表达式?



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?

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);





在此先感谢George



Thanks in advance George

推荐答案

'在哪里'在OrderBy之前执行..所以它不会取消它。 ...



在这里看看这个

http://stackoverflow.com/questions/6700140/proper-way-to-use-linq-with-cancellationtoken [ ^ ]
'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[^]


这篇关于线程结束时退出linq表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆