I have a combo box in main UI where i will display some available items list. I have one background worker thread which will run time check for available items.
I am running this thread in loop so that for each time it will check items available and update the to main UI combobox.
This is my code snippet.
do
{
iNumberofReaders = 0;
this.Dispatcher.BeginInvoke((Action)(() =>
{
NumberOfCards.Items.Clear();
}));
//
// Compose a list of the card readers which are connected to the
// system and which will be monitored.
//
ArrayList availableReaders = this.ListReaders();
this._states = new ReaderState[availableReaders.Count];
for (int i = 0; i <= availableReaders.Count - 1; i++)
{
this._states[i].Reader = availableReaders[i].ToString();
}
result = (SmartcardErrorCode)UnsafeNativeMethods.GetStatusChange(
this._context, 1000, this._states, this._states.Length);
szAvailableReaders = new string[availableReaders.Count];
for (int i = 0; i < availableReaders.Count; i++)
{
if (0 != this._states[i]._attribute)
{
szAvailableReaders[iNumberofReaders] = this._states[i]._reader;
iNumberofReaders++;
} // if
} // for
if (iNumberofReaders > 1)
{
this.Dispatcher.BeginInvoke((Action)(() =>
{
for (int j = 0; j < iNumberofReaders; j++)
{
NumberOfCards.Visibility =system.Windows.Visibility.Visible;
NumberOfCards.Items.Add(szAvailableReaders[j]);
} // for
}));
}
} while (SetThread);
But this code in 2nd iteration throwing
Object reference not set to an instance of an object
An unhandled exception of type 'System.NullReferenceException' occurred
If I comment combobox add element then its working fine.
constant string in add element also throwing same error. so i beleive problem is with something in for loop and dispatcher begininvoke.
NumberOfCards.Items.Add("ABCD");
(NumberOfCards is ComboBox)
I have observed strange behaviour in for loop in dispatcher begin invoke. Can anyone help?