The calling thread can not access this object because a different thread has it \ & ldquo; exception & rdquo;

advertisements

in my project i have text box and when the event fire _rtpAudioChannel_ChannelStateChanged i get this exception The calling thread cannot access this object because a different thread owns it

      void _rtpAudioChannel_ChannelStateChanged(object sender, RtpStateChangedEventArgs<RtpChannelState> e)
      {
            AddNewState("some text here");
      }

      public void AddNewState(string state)
      {
            StatTextBox.Text = state + "\n" + StatTextBox.Text;
      }


For technical reasons, windows and controls created in one thread cannot be accessed from any other thread. To resolve the problem you have to "forward" the control-accessing operation (getting and setting Text) to the appropriate thread, which in WPF is called the dispatcher thread.

Do this by calling StatTextBox.Dispatcher.Invoke (which is synchronous, i.e. does not return until the processing is complete) or StatTextBox.Dispatcher.BeginInvoke (which is asynchronous and offers better performance).