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

后端 未结 6 1307
盖世英雄少女心
盖世英雄少女心 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:51

    I add functions like the following so that I can add items to the list box from either the main thread or background threads. Thi thread checks if a Invoke is necessary and then uses Invoke if it is necessary.

      delegate void AddListItemDelegate(string name,object otherInfoNeeded);
    
      private void
         AddListItem(
            string name,
            object otherInfoNeeded
         )
      {
         if (InvokeRequired)
         {
            BeginInvoke(new AddListItemDelegate(AddListItem), name, otherInfoNeeded
            return;
         }
    
         ... add code to create list box item and insert in list here ...
      }
    

提交回复
热议问题