c# - Cross thread error despite setting ApartmentState -


i trying display content of loop ui. first off, i'm not sure i'm approaching correct way (using winforms) i'm doing:

foreach (string item in stringarray) {         thread thread = new thread(delegate()                               {                                   updatedresultevent(item);                               });         thread.setapartmentstate(apartmentstate.sta);         thread.start(); } 

i hope enough information, if isn't i'll go more detail here.

i have 2 classes, program.cs (winform) , class called logicclass. pass instance of program object logicclass. logicclass has delegate signature matches method in program class. method passed delegate

public void updateresultsonscreen(string newcontent) { txtresults.text += newcontent; } 

the error message is

cross-thread operation not valid: control accessed thread other thread created on

edit

the goal is, similar way progress bar works, see control getting updated in real time. currently, if work, thread doesn't return 'results' screen until thread complete.

use (if on winforms):

public void updateresultsonscreen(string newcontent) {     txtresults.begininvoke(         new action<string>((value) =>         {             txtresults.text += value;         }),         newcontent); } 

also animaonline said, consider using threadpool or parallel.foreach.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -