0
我必須檢查列表框的SelectedIndex
是否在BackgroundWorker
最後一個索引,但由於我正在檢查列表框(這是在GUI線程上)從BackgroundWorker
我得到這個錯誤:檢查是否listbox.SelectedIndex ==從BackBoxWorker的列表框的ListBox.Items.Count
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.'
這是我的代碼:
if (listBox1.SelectedIndex == listBox1.Items.Count)
{
//code here
}
我怎麼可能讓這個if語句工作而不GUI線程上?
第一個人應該*永遠不會*將CheckForIllegalCrossThreadCalls設置爲false。所有這一切都是延遲發現使用來自錯誤線程的控制將不可避免地導致的問題。 –
謝謝,這正是我正在尋找的。當我回家的時候會去測試它! – Thomas
好吧,讓我知道它是否有效,如果這回答你的問題,你可以接受它作爲答案,謝謝! @Thomas –