multithreading - WCF (WPF) error: calling thread must be STA -


i receive data service reference.

the structure f.e. follows:
receive driverdata service reference (namespace: servicereference.driver)
namespace of driverdata in project 'myproject.driver'.

driverusercontrol should created in constructor of myproject.driver.

public driver(int id, string name, string telephone, string plate,                   dictionary<datetime, transporttype> transporttypes, division division)     {         this.id = id;         this.name = name;         this.telephone = telephone;         this.plate = plate;         this.transporttypes = transporttypes;         this.division = division;         usercontrol = new driverusercontrol(this);     } 

but when here:

public driverusercontrol(driver magnet)     {         initializecomponent();          this.magnet = magnet;         render();     } 

whenever reaches constructor of usercontrol following error "the calling thread must sta, because many ui components require this" shows up.

because never started thread anywhere in project don't know how should set sta. guess servicereference seen thread, still, there way change sta?

thanks.

how control instantiated? created when program starts, or listening call coming in wcf service?

normally, main thread wpf or winform app sta ( find stathreadattribute applied main method in code generated file, if search it)

so suspect instancing control in response incoming wcf call. right?

if that's case, have additional concern: ui windows in windows have thread affinity, means thread created them allowed talk them. typically ensured creating windows or controls main thread. background threads should not directly touching members of ui control.

so, need ensure creating user control main thread. easiest way this: if have access form/window user control going placed on, use:

thewindowhostingthecontrol.dispatcher.invoke (or begininvoke, or 1 of asyncinvokes), passing in delegate code instances control.  cause control created on same thread host window has affinity for.   

you need same thing time incoming call web service needs update property on control (of course, use dispatcher instance associated control).

this based on assumption responding incoming wcf call. (sorry if took off track).


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -