-2

Trying to Do a Search in the Background without the UI being Impacted...but I can't seem to find anything to explain how to do invokes or if that's even what i need?

Here's my Code:

Thread backgroundThread = new Thread(
new ThreadStart(() =>
{
    foreach(string file in Directory.EnumerateFiles(@"\\" + environment 
        + @"VRoot\", "*.zip", SearchOption.AllDirectories).Select(Path.GetFileName))
    {
        Console.WriteLine(file);
        //how do i add these files to a ComboBox back on the UI?
    }
    MessageBox.Show("Thread Completed!");
}));

backgroundThread.Start();

Please let me know HOW would i make that call to the origional?

IndieGameDev
  • 2,905
  • 3
  • 16
  • 29
  • Use Control.Invoke https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invoke?view=netcore-3.1 – David Browne - Microsoft Oct 02 '20 at 14:11
  • Maybe that helps: https://stackoverflow.com/questions/661561/how-do-i-update-the-gui-from-another-thread – considere Oct 02 '20 at 14:15
  • 1
    Instead of threads and `Invoke` you could use async/await and `Task.Run`. The idea is to collect the data in the background, and then pass them to the UI thread for updating the UI. The UI will not be blocked because of the `await`. [Here](https://stackoverflow.com/questions/64003533/ui-unresponsive-until-action-is-complete/64003604#64003604) is an example. – Theodor Zoulias Oct 02 '20 at 15:41

1 Answers1

0

Alright Thank you to the both of you...I ended up overthinking this apparently...here's waht i've made and seems to be a good start to what i'm working towards.

Thread backgroundThread = new Thread(
      new ThreadStart(() =>
      {
         TradingPartnerOutComboBox.BeginInvoke(
         new Action(() =>
                   {
                    foreach (string file in Directory.EnumerateFiles(@"\\" + environment + @"VRoot\", "*.zip", SearchOption.AllDirectories).Select(Path.GetFileName))
                          {
                           TradingPartnerOutComboBox.Items.Add(file);
                          }
                     }));
                     tradingPartnerOutPanel.BeginInvoke(
                                new Action(() =>
                                {
                                    tradingPartnerOutPanel.Visible = true;
                                }));
                            MessageBox.Show("Thread Completed!");
                        }));
                    backgroundThread.Start();