C# Can I add values to a listbox with a backgroundwork thread?

后端 未结 6 1310
盖世英雄少女心
盖世英雄少女心 2021-01-03 01:27

I want my background worker to add items to a list box, it appears to do so when debugging but the listbox doesn\'t show the values. I suspect this is something to do with a

6条回答
  •  难免孤独
    2021-01-03 01:49

    You can add them while on a background thread via:

    Form.Invoke
    

    or

    Form.BeginInvoke
    

    which are required to marshall the call from a background thread to a main UI thread. However I'm pretty sure BackgroundWorker offers an event that automatically gets called on the Foreground thread and you should be able to update on this event without any problems. This is "ProgressChanged" which can be fired by the background worker process by calling ReportProgress.

    Have you tried calling .Refresh() on the listbox as well?

提交回复
热议问题