Click here to Skip to main content
15,895,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I refrain error <<cross thread="">> ,I've encountered this problem a lot of time :(
C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            backgroundWorker1.RunWorkerAsync();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {


            s:
            if (button1.Left != 0)
            {
    button1.Left--; //Cross-thread operation not valid ! why it happend
                Thread.Sleep(20);
            }
            else
            {
                button1.Left++;
                Thread.Sleep(20);
                if (button1.Left == 250)
                    goto s;


            }

        }


    }


please help me bros. :doh:
what's the solution of these type of problems...?
Posted
Updated 14-Mar-10 22:06pm
v3

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,

C#
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();
       }

    }
 
Share this answer
 
You can only access GUI elemnets (i.e. controls, forms, etc from the thread that created them. In your case, "button1" is a GUI element, so the BackgroundWorker thread cannot access it directly without throwing an exception.

Use
button1.Invoke(delegate {button1.Left++; });


Additionally it is a good idea to wrap such code in
if (button1.InvokeRequired)
   {
   ...
   }
just as a check.
 
Share this answer
 
You cannot interact with UI elements in your background worker thread. You can send progress events which will fire on the UI thread and change your UI there. There is a way to turn this off, but you're better off working with it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900