views:

89

answers:

2

"If necessity is the mother of invention, I'd like to kill the guy who invented this."

-Jimmy Buffet

I don't care if 100 people with a million reputation jump down my throat for saying this, but who ever thought of throwing this exception deserves a fate worse than death.

I have QUITE LITERALLY copied and pasted code from one project to another to populate standard data into a small set of text boxes. In one application, the data is displayed perfectly. The new one I'm working on now, throws this exception of course.

So here is my question - why would one throw it and the other not if the code is the same? Is there a setting in the project settings? Is there a property in the text box that prevents this?

I don't recall setting anything in the project to ignore this exception, but the code is EXACTLY THE SAME.

+3  A: 

STAThread attribute set on the thread creating the controls?

Is the populating code called in the UI thread or not? Noone cares whether you did copy the code properly - if the call is on the wrong thread to start with.

TomTom
How should the STAThread attribute be set?
Jim Fell
Well, it does not have to be set. It should be on the Main method, and other wise set the any other thread's appartement to STA: thread.SetApartmentState(ApartmentState.STA);
TomTom
A: 

I don't want to repeat the error message, but you are trying to modify a control from a thread different than th thread that control was created on.

Take a look at InvokeRequired and Invoke.

mlsteeves