title: VB.NET Delegates and Invoke - can somebody explain these to me?
body: <p>I'm new to the world of threading, but a few aspects of an app I'm working on require me to use a BackgroundWorker control to prevent the UI freezing up while it's doing some file operations.</p>
  
  <p>What I'm trying to do is update a couple of form labels from within the BackgroundWorker.  Having never worked with this before I very quickly discovered that I can't access controls that weren't created within the same thread, so after a bit of research I've implemented the following code that seems to make everything work:</p>
  
  <pre><code>Private Delegate Sub DelegateUpdateStatus(ByVal statusText As String, ByRef currentFile As String)
  
  Private Sub UpdateStatus(ByVal statusText As String, ByVal currentFile As String)
  
      If InvokeRequired Then
          Invoke(Sub() LblStatus.Text = statusText)
          Invoke(Sub() LblCurrentFile.Text = currentFile)
      Else
          LblStatus.Text = statusText
          LblCurrentFile.Text = currentFile
      End If
  
  End Sub
  </code></pre>
  
  <p>Thing is though, I have no understanding of what this code is doing, or why it's required.</p>
  
  <p>I've done a bit of research but I've not done any real work with this kind of thing, and most articles I've read assume some kind of prior knowledge.</p>
  
  <p>The three main things I looking to gain an understanding of:</p>
  
  <ul>
  <li>Why this code is required (as in, why the controls can't be accessed directly from the BackgroundWorker)</li>
  <li>What a delegate is, and when the use of it is required</li>
  <li>What the Invoke method does, and what I'm checking for with InvokeRequired</li>
  </ul>
  
  <p>As said, threading is still quite a foreign concept, so any answers in plain English would be really helpful - thanks!</p>
  
  <p>EDIT: Thanks for the responses so far everybody.  I've done some further reading, and I'm wondering if I'm going about this the right way.  The reason I'm using a BackgroundWorker is to ensure that the UI remains responsive while I'm performing file operations.  The issue is, I still need to wait until the BackgroundWorker has done its job so I can return a boolean indicating the success of the operation.  There's ways to work around this, but from my reading, having to wait for a BackgroundWorker to complete its work is defeating the purpose of using it in the first place.  So, what's the best way to go about preventing the UI from locking up?</p>