You cannot update your user interface from a different thread than the UI's own thread. If you want to update the UI, then create a property which can be updated by the backgroundworker, then periodically check to see if that property has been updated by using a timer or some such, and then change the UI according to that property. One other thing, please do not use GOTO in any situation, but especially in multi thread
applications, EVER!
Try this,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int intLeft = 0;
public int MoveLeft
{
get { return intLeft; }
set { intLeft = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
timer1.Start();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 50; i++)
{
MoveLeft = i;
Thread.Sleep(200);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SetUI();
}
private void SetUI()
{
button1.Left = MoveLeft;
}
private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
timer1.Stop();
timer1.Dispose();
}
}