wpf application线程需要注意的地方

foreach(var item in list)

{

   Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        SimpleList.Add(m);
                    }), System.Windows.Threading.DispatcherPriority.Background);

}

 

此时可能导致添加相同的item,可能是linq的延迟特性所导致(不确定)

 

此时需要特例化,即用for循环代替foreach

 

            for (var i = 0; i < list.Count; i++)
            {
                var m = list[i];
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        SimpleList.Add(m);
                    }), System.Windows.Threading.DispatcherPriority.Background);
            }

 

即可添加真正的元素。

你可能感兴趣的:(application)