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

Capture and play voice with Asterisk ARI -

java - Why database contraints in HSQLDB are only checked during a commit when using transactions in Hibernate? -

visual studio - Installing Packages through Nuget - "Central Directory corrupt" -