12 Replies - 5693 Views - Last Post: 30 October 2010 - 02:24 AM Rate Topic: ***** 1 Votes

#1 Chronicade  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Cross-thread operation not valid: Control

Posted 28 October 2010 - 08:59 PM

Do not know what I did to cause the problem, but now I get this problem on this line of code. Maybe one of the settings to the datagridview3 that I changed caused it? Completely baffled. A button triggers a timer, which then ticks to start the backgroundworker. Inside that bgw is this code, I have not touched anything in the code since it last worked, just modified a few datagridview options and made a few other datagridviews.
 if (loopednowupdate == "1")
                    {
                        string[] rowzx = new string[] { UrlDecode(Charnamex), Levelsx, Skillsx, HGsx, Favorsx, Incomesx, tb36, BotStuff, "", "" };
                        dataGridView3.Rows[CurrentBot].SetValues(rowzx);
                    }
                    else
                    {
                        string[] rowz = new string[] { UrlDecode(Charnamex), Levelsx, Skillsx, HGsx, Favorsx, Incomesx, tb36, BotStuff, "", "" };
                        dataGridView3.Rows.Add(rowz);//<------------THIS LINE!
                    }


This post has been edited by Chronicade: 28 October 2010 - 09:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cross-thread operation not valid: Control

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 12:26 AM

Mostly the Exception is because of an access of an object which is outside of the DoWork.
When you want to add a Row to the GridView use Invoke: Control.Invoke Method (Delegate)
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 12:58 AM

A background worker is on a different thread to the form.
Was This Post Helpful? 2
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 04:54 AM

You can't access a UI Control from a background thread.
Was This Post Helpful? 1
  • +
  • -

#5 Chronicade  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 05:11 AM

Yeah I know all of that, but I would like to know what would cause this to error when it has been working for a while. Small things changed too.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 05:37 AM

Maybe it wasn't being run in the DoWork event of the BackgroundWorker? Maybe it was in the RunWorkerCompleted Event and was moved into the DoWork event? All I know is, that code will fail everytime.
Was This Post Helpful? 2
  • +
  • -

#7 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 05:52 AM

Well as the other guys have said it is because you are changing an object belonging to the UI thread on a background thread. On the line
dataGridView3.Rows.Add(rowz);
you are adding a row to the data grid. This is changing the object. Maybe you didn't used to do it this way, but this line will always have caused an error from a background thread, because you are modifying a UI object from a thread other than that which it was created on. You can use the Invoke() method of a control to update it from a different thread...
// use .Invoke() to invoke an update to a UI object belonging to a different thread. Add this where you need it in the DoWork() code
this.dataGridView3.Invoke(new UIUpdaterDelegate(() =>
{
    // add
    this.dataGridView3.Rows.Add(rowz);

}));

// I used this delegate to update the code... add it to your namespace
delegate void UIUpdaterDelegate();


Hope that helps
Was This Post Helpful? 1
  • +
  • -

#8 Chronicade  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 05:58 AM

Right as soon as I finish modifying some google code on Invoking. I get a new email and you post a copy. Awesome :) Ill give it a shot. Thanks for all the help, but seriously I have not touched the backgroundworker at all, only things on the main thread. My other backgroundworkers can still actively modify the main thread. O_O
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 06:26 AM

We also have this tutorial on the subject.
Was This Post Helpful? 1
  • +
  • -

#10 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 06:51 AM

Perhaps it was the first time, that loopednowupdate was not 1 ;)
Was This Post Helpful? 1
  • +
  • -

#11 Chronicade  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Re: Cross-thread operation not valid: Control

Posted 29 October 2010 - 10:52 PM

Awesome, the code work, mucho kudos. So, a lot of my code is done in backgroundworkers and numerous parts involve datagridviews. My question is for some reason without me having to invoke anything it works, but a little weird. Sometimes it does what it is supposed to with a few problems, then other times it seems to work fine. Would this be a result to me having a buggy call from a backgroundworker that is accessing the main thread?

This post has been edited by Chronicade: 29 October 2010 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#12 elbielefeld  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 216
  • Joined: 18-May 10

Re: Cross-thread operation not valid: Control

Posted 30 October 2010 - 02:20 AM

Maybe you have set the CheckForIllegalCrossThreadCalls property of the form to false.
Was This Post Helpful? 1
  • +
  • -

#13 Chronicade  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 05-September 09

Re: Cross-thread operation not valid: Control

Posted 30 October 2010 - 02:24 AM

Nope :/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1