Ive got an issue where the first item in the collection is responding to an update but no others (out of 40). Ive had a look around the net for answers but unfortunately Im still getting nowhere after a couple of days.
The calling code which kicks off a thread for the detection loop:
_detectionThread = new Thread(() => _x.StartDetection());
_detectionThread.Start();
Ive got the following code in one of my helper classes which simply polls and when something is detected, by way of an event the View-Model is called:
public event EventHandler SomethingIsDetected;
private void OnSomethingDetected()
{
if (SomethingIsDetected!= null)
{
SomethingIsDetected(this, new EventArgs());
}
}
Code for detection loop:
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);
}
Code for updating items. View is bound to properties here (especially CurrentItem). Items is an 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));
}
While Im stepping through (with the help of a debugging converter) I notice that the item is udpated just the first time. The rest just loops through. Ive setup property CurrentItem with a DependencyProperty.
Ive tried using CheckAccess to use Delegate and udpate property and that didnt help either.
Any help is welcome and thanks!