title: Update a label from a task
body: <p>I'm trying to implement tasks in my program. I launch a task that will produce a log file, and after, I want to update the label to say "Log sucessfully saved".</p>
  
  <p>Here is my code</p>
  
  <pre><code>Private Function Createlog(ByVal mylist As List(Of classTest))
          Dim sw As New StreamWriter("log_list.log")
          For index = 1 To mylist.Count - 1
              sw.WriteLine(mylist(index).comments)
          Next
          sw.Close()
          Try
              Me.Invoke(UpdateLabel("Log sucessfully saved"))
          Catch ex As Exception
  
          End Try
          Return 1
      End Function
  
      Private Function UpdateLabel(ByVal text As String)
          Label1.Text = text
          Return 1
      End Function
  </code></pre>
  
  <p>I launch the task from the Main form in the Load() :</p>
  
  <pre><code> Dim tasktest = Task(Of Integer).Factory.StartNew(Function() Createlog(theList))
  </code></pre>
  
  <p>(I don't know if it is better to use the factory or declare as a task and then task.Start())</p>
  
  <p>I have the error on the label update : </p>
  
  <blockquote>
    <p>Cross-thread operation not valid: Control 'Label1' accessed from a thread
    other than the thread it was created on.</p>
  </blockquote>
  
  <p>Could you please explain why it doesn't work with the invoke method ? And do you have an alternative solution ?</p>
  
  <p>Thanks for your help</p>