13 Replies - 8888 Views - Last Post: 05 June 2009 - 08:47 AM Rate Topic: -----

#1 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Cross-thread operation not valid

Posted 02 June 2009 - 04:07 PM

hi all,

what's mean of this error?
error:
Cross-thread operation not valid: Control 'iterationBox' accessed from a thread other than the thread it was created on.

thx
Is This A Good Question/Topic? 0
  • +

Replies To: Cross-thread operation not valid

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Cross-thread operation not valid

Posted 02 June 2009 - 04:14 PM

You have to turn a property to false in order for it to work. I don't really remember what it is exactly.

Maybe, allowcrossthreadoperations = false;

Something like that.

Six will probably help you out :P
Was This Post Helpful? 0
  • +
  • -

#3 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 02 June 2009 - 04:46 PM

View Postpapuccino1, on 2 Jun, 2009 - 03:14 PM, said:

You have to turn a property to false in order for it to work. I don't really remember what it is exactly.

Maybe, allowcrossthreadoperations = false;

Something like that.

Six will probably help you out :P


where should i write that property on?

ok, six help me bro............... :D

thx pap..
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Cross-thread operation not valid

Posted 02 June 2009 - 04:55 PM

Actually that's happening because you're trying to access a GUI control from a thread that didn't create it (the applications main thread), which isn't allowed directly. Have a look at this tutorial on Cross thread communication that may help :)
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Cross-thread operation not valid

Posted 02 June 2009 - 05:15 PM

Psycho, can you believe I STILL can't figure out how to have my program search the web without freezing my GUI. Dammit!
Was This Post Helpful? 0
  • +
  • -

#6 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 02 June 2009 - 06:36 PM

where should i take public void delegate......, anywhere i token it having error :(
Was This Post Helpful? 0
  • +
  • -

#7 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Cross-thread operation not valid

Posted 02 June 2009 - 11:27 PM

I didn't start doing hardcore c# until the most recent .NET Framework 3.5, so I've grown accustomed to using anonymous methods instead of delegates. You can look into the MethodInvoker delegate, or you can try wrapping your iterationBox access with Control.Invoke like so:

Invoke((Action)(() => iterationBox.Text = "Zombies ahead!"));



Invoke() switches context to the thread that owns the control that called Invoke and executes an action...meaning if I wrote iterationBox.Invoke(...), the program will transfer control to the thread that owns iterationBox, and that thread will perform the action. It would be more accurate to use iterationBox.Invoke(), but I usually just call "Invoke" since it refers to my Form instance, which contains all my controls anyway.
Was This Post Helpful? 0
  • +
  • -

#8 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 03 June 2009 - 05:00 AM

View Postmasteryee, on 2 Jun, 2009 - 10:27 PM, said:

I didn't start doing hardcore c# until the most recent .NET Framework 3.5, so I've grown accustomed to using anonymous methods instead of delegates. You can look into the MethodInvoker delegate, or you can try wrapping your iterationBox access with Control.Invoke like so:

Invoke((Action)(() => iterationBox.Text = "Zombies ahead!"));



Invoke() switches context to the thread that owns the control that called Invoke and executes an action...meaning if I wrote iterationBox.Invoke(...), the program will transfer control to the thread that owns iterationBox, and that thread will perform the action. It would be more accurate to use iterationBox.Invoke(), but I usually just call "Invoke" since it refers to my Form instance, which contains all my controls anyway.


i'm using .NET 2.0.
i'm trying using what psycho gave in tutorial, but i'm still confuse , should invoke method called for all controls in my form?
Was This Post Helpful? 0
  • +
  • -

#9 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 03 June 2009 - 04:21 PM

shifted.....
Was This Post Helpful? 0
  • +
  • -

#10 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Cross-thread operation not valid

Posted 03 June 2009 - 10:15 PM

You're not being shafted, you just need to give us time to reply.

You need to call Invoke or BeginInvoke anytime you're trying to access a GUI control from another thread. So let's say your form and all its controls are on the UI thread (or main thread), and you launch a backgroundworker, and that backgroundworker's DoWork method needs to update a textbox. If you're using the ISynchronizeInvoke interface from the psycho's tutorial, then you need to declare your ISynchronizeInvoke variable, set it to the control you need to access, and call the Invoke method with your delegate and an array of objects that represents that parameters that your delegate requires, if any. So following the tutorial, you would pass a string object, and int object into the array.
Was This Post Helpful? 0
  • +
  • -

#11 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 04 June 2009 - 02:51 AM

View Postmasteryee, on 3 Jun, 2009 - 09:15 PM, said:

You're not being shafted, you just need to give us time to reply.

You need to call Invoke or BeginInvoke anytime you're trying to access a GUI control from another thread. So let's say your form and all its controls are on the UI thread (or main thread), and you launch a backgroundworker, and that backgroundworker's DoWork method needs to update a textbox. If you're using the ISynchronizeInvoke interface from the psycho's tutorial, then you need to declare your ISynchronizeInvoke variable, set it to the control you need to access, and call the Invoke method with your delegate and an array of objects that represents that parameters that your delegate requires, if any. So following the tutorial, you would pass a string object, and int object into the array.


thanks, it works. :)
but, i get error:
Cannot access a disposed object.

what's mean of this error.
thanks :)
Was This Post Helpful? 0
  • +
  • -

#12 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Cross-thread operation not valid

Posted 04 June 2009 - 08:49 AM

That probably means you're trying to access one of your controls that you disposed. If you close a second form and then try to access it, you may get this error because all the resources held by the second form was released on close. You can try calling textBox1.IsDisposed to validate your control first prior to using it, or you can just rearrange your code a little bit to avoid this error.
Was This Post Helpful? 0
  • +
  • -

#13 izzahaf  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 112
  • Joined: 12-March 09

Re: Cross-thread operation not valid

Posted 04 June 2009 - 08:23 PM

View Postmasteryee, on 4 Jun, 2009 - 07:49 AM, said:

That probably means you're trying to access one of your controls that you disposed. If you close a second form and then try to access it, you may get this error because all the resources held by the second form was released on close. You can try calling textBox1.IsDisposed to validate your control first prior to using it, or you can just rearrange your code a little bit to avoid this error.


i'm sorry, i don't understand. can u give an example? :D

thanks
Was This Post Helpful? 0
  • +
  • -

#14 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Cross-thread operation not valid

Posted 05 June 2009 - 08:47 AM

    public partial class Form1 : Form
    {
        Form form2;
        public Form1()
        {
            InitializeComponent();
            form2 = new Form();
            form2.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form2.Close(); // simulate the user closing the window
            if (form2.IsDisposed)
                MessageBox.Show("Uh oh");
            else
                form2.Show();
        }
    }



I load a 2nd form. When i click the button on the 1st form, it closes the 2nd form, at which point all the component resources of the 2nd form are disposed/released and inaccessible. This is essentially the same thing that happens when you manually click on the 'X' close button in the upper right corner of your 2nd form. This means form2.IsDisposed is true and my messagebox will popup. If I called form2.Show() right after form2.Close() without checking isDisposed, then this will reproduce the error that you ran into (Cannot access a disposed object). The error will also tell you what type of object you were trying to access:

Cannot access a disposed object.
Object name: 'Form'.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1