c# - Refresh the user interface while a separated STA thread is adding many controls -
i add pushpins on map means of thread. thread sta , use dispatcher.
the issue map not refreshed pushpins. no pushpin appears on map.
i've class named "serialinterf" reads serial port. each reading invokes event 'datareceivedhandler':
private void datareceivedhandler(object sender, serialdatareceivedeventargs e) { serialport port = (serialport)sender; string data = port.readexisting(); latlong message = getdata(data); serialinterfeventargs arg = new serialinterfeventargs(message); this.messagereceived(this, arg); }
the event "datareceivedhandler" invokes event of same class called "messagereceived". class serialinterf instancied mainwindow class. in last one, event "messagereceived" defined:
transm.messagereceived += new trans.serialinterfeventhandler(writetrace);
the method writetrace launches thread (sta):
private void writetrace(object sender, transmissioneventargs e) { // on crée un thread car on ne peut pas modifier l'utilisateur interface avec un thread mta (par défaut). thread thread = new thread(() => test(e.message)); thread.setapartmentstate(apartmentstate.sta); thread.start(); }
this thread launches method "test" must create pushpins on map:
private void test(latlong message) { this.dispatcher.invoke(new action(() => { pushpin pin = new pushpin(); pin.location = new location(message.latitude, message.longitude); map.children.add(pin); })); }
the code
this.dispatcher.invoke(new action(() =>
has been modified by
this.map.dispatcher.invoke(new action(() =>
that's ok !
see: cross-thread
i don't think need sta thread.
Comments
Post a Comment