レコードを送信しているBluetoothデバイスから通知を取得しようとしていて、UIを定期的に更新してレコードを表示しようとしています。以下のwhileループはUI更新を処理するために独自のスレッド内にあり、残りのモジュールは他のタスクを処理します。 gattCallback
は、BluetoothGattCallback
クラスのインスタンスで、受信したレコードのリストを追加し、getHistory()
が呼び出されたときにそのリストを返します。foreachループでコレクションが変更されました
私の問題は、私はforeach
ラインをヒットしたときに、非常に多くの反復の後、私はエラーが出るということです。
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
私の知る限り
、history
はここかどこか他で更新されていないが、私の私はエラーで混乱しています。私は具体的にはforeach
の間に修正を避けるために、記録履歴のコピーをgetHistory()
で検索します。誰かがそれを引き起こしているかもしれないと示唆するためのヒントを提案することはできますか?
これはAndroid 6.1.1のMoto G PlayからAndroid 7.1.1のMoto E4に切り替えると問題が発生したことが関係している可能性があります。
// Periodically check to see what needs updating
while (!finishedDisplayThread)
{
// See if there are any new records to display
int count;
List<Record> history = gattCallback.getHistory();
if (history == null)
{
count = 0;
}
else
{
count = history.Count;
}
// Only update the display if it has changed
if(count != prevCount)
{
prevCount = count;
List<string> recordList = new List<string>();
if (history == null)
{
recordList = new List<string>();
recordList.Add("No history.");
}
else
{
foreach (Record record in history)
{
recordList.Add(record.ToRow());
}
}
//Update the display
RunOnUiThread(() =>
{
ListAdapter = new ArrayAdapter<string>(this,
Resource.Layout.ListItemLayout,
recordList);
recordCountText.Text = "" + count;
});
}
Thread.Sleep(100);
}
えーえは、総理にかなっているあなたに感謝します。それが私の埋め込まれた終わりだったら、それは私が考えた最初のことです!それは今治療を働く:) – Hester