我需要幫助: 我已經與相關主題嵌套對象:選擇<Thread>對象使用LINQ
public class XNodeViewModel
{
private int id;
private Thread workerThread;
private bool _alivingThread;
readonly ObservableCollection<XNodeViewModel> _children;
private XNodeViewModel(...)
{
...
if (...)
{
workerThread = new Thread(DoWork) { IsBackground = true };
workerThread.Start();
}
}
public ObservableCollection<XNodeViewModel> Children
{
get { return _children; }
}
public int Level
{
get { return _xnode.Level; }
}
public Thread WorkerThread
{
get { return this.workerThread; }
}
}
在WPF後面的代碼我這個視圖模型的引用,我想所有的線程obects相關。 我正在學習LINQ和我知道,有功能的SelectMany扁平化嵌套對象: 一個按鈕,我想停止使用此功能的所有主題:
public void StopAllThread()
{
//_firstGeneration is my root object
var threads = _firstGeneration.SelectMany(x => x.WorkerThread).ToList();
foreach(thread in threads){
workerThread.Abort();
}
}
但是,編譯器告訴我:
錯誤1無法從用法推斷方法'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable,System.Func>)'的類型參數。嘗試明確指定類型參數。
只有當我請求鍵入「線程」(如果我要求另一種類型的對象是好的) 我在哪裏做錯了?
嘗試使用Select而不是SelectMany –
請注意,在這種情況下,不應使用「Abort」。在特殊情況下應使用「中止」。考慮使用'Join'來代替'CancelationToken'。 – oleksii