title: backgroundworker gives a cross-thread exception when ProgressChanged?
body: <p>i have file transfer application (Server-Client) TCP [.net 4]
  why is backgroundworker gives me an exception in (backgroundWorker1_ProgressChanged)</p>
  
  <p>The client send command that has a <b>destination path</b> (where file should saved into) and the <b>size of file</b> to the Server to start receiving that file .. so once the server takes that command .. it will call :</p>
  
  <pre><code>fileTransfer1.Receive(destPath, fileSize);
  </code></pre>
  
  <p>this method run in its own thread in form1.cs :</p>
  
  <pre><code>private void Job(object o)
      {
  
          Socket client = (Socket)o;
          NetworkStream stream = new NetworkStream(client);
          StreamReader sr = new StreamReader(stream);
  
          string cmd = null;
  
          while ((cmd = sr.ReadLine()) != null)
          {
              string[] command = cmd.Split('&lt;');
              switch (command[0])
              {
                 case "receive":
                              fileTransfer1.Receive(command[1], Convert.ToInt64(command[2]));
                              break;
                              default:
                              break;
              }
          }
  </code></pre>
  
  <p><b>fileTransfer1 (userControl)</b> :</p>
  
  <pre><code>    public void Receive(string destPath, long fileSize)
      {
              List&lt;object&gt; job = new List&lt;object&gt;();
              job.Add(destPath);
              job.Add(fileSize);
              backgroundWorker1.RunWorkerAsync(job);
      }
  
      long sum = 0;
      long fileSize = 0;   //this will equal the size of the file later.
      private void ReceiveFile(string destPath, long fileSize)
      {
          using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
          {
              try
              {
                  int count = 0;
                  data = new byte[packetSize];
                  while (sum &lt; fileSize)
                  {
                      count = network.Read(data, 0, packetSize);
                      fs.Write(data, 0, count);
                      sum += count;
                      backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
                  }
              }
              finally
              {
                  CloseTransfer();
              }
          }
      }
  </code></pre>
  
  <p>here's backgroundworker method :</p>
  
  <pre><code>    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
      {
           List&lt;object&gt; job = (List&lt;object&gt;)e.Argument;
           string destPath = (string)job[1];
           long fileSize = (long)job[2];
           ReceiveFile(destPath, fileSize);
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
          progressBarFile.Position = e.ProgressPercentage;
          progressBarFile.Text = progressBarFile.Position.ToString() + "%";
    /*here*/  labelRemaining.Text = CnvrtUnit(fileSize - sum);  
      }
  </code></pre>
  
  <p>my problem that i got an exception <b>(Cross-thread operation not valid: Control 'labelRemaining' accessed from a thread other than the thread it was created on.)</b>
  that references to this line:</p>
  
  <pre><code> labelRemaining.Text = CnvrtUnit(fileSize - sum);
  </code></pre>
  
  <p>i know about that exception and i know that i have to use (delegate/invoke) thing .. but.. i know that backgroundworker meant for this .. plus .. i have the same code but The server here Send a file to the client .. and it doesn't give that exception .. it works fine and the form shows me the details as it should be.<br>
  <B>So why do i get this exception just when i receive a file ??</b><br> note : receive method works fine.</p>