title: How can I run code in a background thread and still access the UI?
body: <p>I made a file search program in visual studio on <strong>windows 10</strong> using .net lang,
  My problem starts from form1 with a "<code>dim frm2 as form2 = new form2</code>" call,
  after the new form being shown i start a while loop on form1 that feeds data into a listbox in form 2:</p>
  
  <blockquote>
    <p>1)form1 call form2 and show it.</p>
    
    <p>2)form1 start a while loop.</p>
    
    <p>3)inside the while loop data being fed to listbox1 in frm2</p>
  </blockquote>
  
  <p>Now everything works on <strong>windows 10</strong>, the while loop can run as much as it needs without any trouble, the window can loose focus and regain focus <strong>without</strong> showing any <code>"Not Responding.." msgs or white\black screens..</code></p>
  
  <p><strong>But,</strong> when i take the software to my friend computer which is running <strong>windows 7</strong>, install all required frameworks and visual studio itself, run it from the .sln in debug mode, and do the same search on the same folder the results are:</p>
  
  <blockquote>
    <p><strong>1)</strong> the while loop runs smoothly as long as form 2 dont loose focus
    (something that doesnt happen on windows 10)</p>
    
    <p><strong>2)</strong> when i click anywhere on the screen the software loose focus what
    causes <strong>1)</strong> to happen (black screen\white screen\not responding etc..)</p>
    
    <p><strong>3)</strong> if i wait the time needed for the loop and <strong>dont</strong> click anywhere else
    it keeps running smoohtly, <strong>updating</strong> a label <strong>like it should</strong> with the
    amount of files found.. and even finish the loop with <strong>100% success</strong>
    (again unless i click somewhere)</p>
  </blockquote>
  
  <p><strong>Code Example:</strong></p>
  
  <pre><code>Sub ScanButtonInForm1()
      Dim frm2 As Form2 = New Form2
      frm2.Show()
      Dim AlreadyScanned As HashSet(Of String) = New HashSet(Of String)
      Dim stack As New Stack(Of String)
      stack.Push("...Directoy To Start The Search From...")
      Do While (stack.Count &gt; 0)
          frm2.Label4.Text = "-- Mapping Files... -- Folders Left:" + stack.Count.ToString + " -- Files Found:" + frm2.ListBox1.Items.Count.ToString + " --"
          frm2.Label4.Refresh()
          Dim ScanDir As String = stack.Pop
          If AlreadyScanned.Add(ScanDir) Then
              Try
                  Try
                      Try
                          Dim directoryName As String
                          For Each directoryName In System.IO.Directory.GetDirectories(ScanDir)
                              stack.Push(directoryName)
                              frm2.Label4.Text = "-- Mapping Files... -- Folders Left:" + stack.Count.ToString + " -- Files Found:" + frm2.ListBox1.Items.Count.ToString + " --"
                              frm2.Label4.Refresh()
                          Next
                          frm2.ListBox1.Items.AddRange(System.IO.Directory.GetFiles(ScanDir, "*.*", System.IO.SearchOption.AllDirectories))
                      Catch ex5 As UnauthorizedAccessException
                      End Try
                  Catch ex2 As System.IO.PathTooLongException
                  End Try
              Catch ex4 As System.IO.DirectoryNotFoundException
              End Try
          End If
      Loop
  End Sub
  </code></pre>
  
  <p><strong>My conclusions was simple!</strong></p>
  
  <blockquote>
    <p>1) <strong>windows 7</strong> dont support live ui (label) update from a while loop
    called from a button...</p>
    
    <p>2) windows 7 could possibly support a new
    thread running the same loop</p>
  </blockquote>
  
  <p>i think mabye if i run all the code in a thread mabye the ui will remain responsive </p>
  
  <blockquote>
    <p>(<em>by the way the UI is not responsive in <strong>windows 10</strong> but i still see
    the label refresh and <strong>nothing crashes when form loose focus</strong>..</em>)</p>
  </blockquote>
  
  <p>so i know how to do that but i also know that if i do that a thread will not be able to update a listbox or a label in a form and refresh it..</p>
  
  <p>so the thread will need to update an external file with the data and the form2 will need to read that data live from the file but will it make the same problems? i have no idea what to do.. can use some help and tips. <strong><em>THANK YOU!</em></strong></p>
  
  <blockquote>
    <p>I must menttion the fact that the loop is working on windows 10 without a responsive UI means i cant click on any button but i can
    still see the label refresh BUT on windows 7 everything works the same
    UNLESS i click somewhere, no matter where i click on windows the loop
    crashes</p>
    
    <p>im using framework 4.6.2 developer</p>
  </blockquote>