0

I would like to add pushpins on my map by means of a thread. The thread is STA and I use a dispatcher.

The issue is my map is not refreshed with the pushpins. No pushpin appears on the map.

I've a class named "SerialInterf" which reads a serial port. Each reading invokes the 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 another event of the same class called "MessageReceived". The class SerialInterf is instancied by my MainWindow class. In this last one, the event "MessageReceived" is defined:

transm.MessageReceived += new Trans.SerialInterfEventHandler(WriteTrace);

The method WriteTrace launches the 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 the method "test" which must create my pushpins on my 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);
            }));
        }
profou
  • 197
  • 1
  • 17

1 Answers1

0

The code

this.Dispatcher.Invoke(new Action(() => 

has been modified by

this.map.Dispatcher.Invoke(new Action(() => 

That's ok now !

See: Cross-thread

I don't think that I need STA thread.

Community
  • 1
  • 1
profou
  • 197
  • 1
  • 17