You cannot access your ListView element from a background thread. You need to do this in the user interface thread.
There are two options: first, you can switch this to use Windows.Forms.Timer to run every 30 seconds. This will happen in the user interface thread, which will completely fix this problem, but move the processing to the user interface thread. If your processing is slow, it can lead to a hang.
Alternatively, use Control.Invoke to redirect calls in the ListView back to the user interface stream:
listView2.Invoke(new Action( () => listView2.Items.Clear() ) );
In any case, I would rethink the use of BackgroundWorker. It is not intended for “temporary events” that occur at regular intervals. You should consider switching to Windows.Forms.Timer (UI thread) if long processing is not performed, or System.Timers.Timer (running in the background thread) if processing takes some time. This is a better design option than BackgroundWorker, which never ends.
Reed Copsey Apr 05 '10 at 17:32 2010-04-05 17:32
source share