我遇到了一个问题,即集合中的第一个项目正在响应更新,但没有其他项目(满分为40)。我看了一遍网络寻找答案,但不幸的是,在几天之后,我仍然没有得到任何帮助。螺纹应用程序问题
揭开序幕一个线程用于检测循环调用代码:
_detectionThread = new Thread(() => _x.StartDetection());
_detectionThread.Start();
Ive得到了我的助手类的一个下面的代码简单地投票和当检测到的东西,通过事件的方式视图模型被称为:
public event EventHandler SomethingIsDetected;
private void OnSomethingDetected()
{
if (SomethingIsDetected!= null)
{
SomethingIsDetected(this, new EventArgs());
}
}
代码检测回路:
var startCheckTime = DateTime.Now;
var nextCheck = startCheckTime.AddSeconds(PollingInterval.TotalSeconds);
while (_performDetection)
{
startCheckTime = DateTime.Now;
if (startCheckTime >= nextCheck)
{
nextCheck = startCheckTime.AddSeconds(PollingInterval.TotalSeconds);
{
var detectionTask = Task.Factory.StartNew(() => IsXConnected());
IsXPresent = detectionTask.Result;
Thread.Sleep(TimeSpan.FromSeconds(1));
if (IsXPresent)
{
Application.Current.Dispatcher.Invoke(new Action(OnSomethingDetected));
}
}
}
Thread.Sleep(10);
}
更新项目的代码。视图绑定到这里的属性(特别是CurrentItem)。项目是一个ObservableCollection
foreach (var item in Items) //loop through 40 items
{
//do some operation then set the current item
Application.Current.Dispatcher.Invoke(new Action(() => CurrentItem = item));
}
虽然林步进通过(与调试器的帮助下)我注意到,该项目被udpated只是第一次。剩下的只是循环。我用一个DependencyProperty设置属性CurrentItem。
我曾尝试使用CheckAccess来使用Delegate和udpate属性,并没有帮助。
任何帮助,欢迎和感谢!
1)何苦因为您在下一行阻塞,所以创建一个调用IsXConnected的任务,直到操作完成。 2)什么是IsXConnected在做什么?既然这就是我在第一个结果后返回错误,这似乎很重要。3)我意识到foreach循环中有一些代码设置CurrentItem = item,但是一开始它并没有任何意义,因为除非有阻塞调用,否则它会很快通过所有的Items和CurrentItem将最终成为Items.Last,这将是UI将会看到的唯一的东西。 –
感谢您的保罗,我得到了一个UI更新位的Debug.WriteLine,这表明只有第一个项目正在调用更新位。 (即使通过调用方法来处理点1,而没有任务)。 – TheRenoRanger
我相信从MainWindow.XAML.cs的构造函数执行线程是罪魁祸首。签出[这](http://stackoverflow.com/questions/6639237/wpf-custom-control-dependencyproperty-issue)发布更多信息。 – TheRenoRanger