title: How to handle c# WPF thread in MVVM view model
body: <p>I am having a bear of a time figuring out how to handle a Thread from a class outside my ViewModel.</p>
  
  <p>The Thread originates from a <code>Track</code> class. Here is the <code>ResponseEventHandler</code> code in <code>Track</code>:</p>
  
  <pre><code>public delegate void ResponseEventHandler(AbstractResponse response);
  public event ResponseEventHandler OnResponseEvent;
  </code></pre>
  
  <p>When a "command" method is processed from within my <code>Track</code> object, the following code runs the <code>OnResponseEvent</code>, which sends a message in a Thread back to my ViewModel:</p>
  
  <pre><code>if (OnResponseEvent != null)
  {
      OnResponseEvent(GetResponseFromCurrentBuffer());
  }
  </code></pre>
  
  <p><code>GetResponseFromCurrentBuffer()</code> merely returns a message type which is a pre-defined type within the <code>Track</code>.</p>
  
  <p>My <code>MainWindowViewModel</code> constructor creates an event handler for the <code>OnResponseEvent</code> from the <code>Track</code>:</p>
  
  <pre><code>public MainWindowViewModel()
  {
      Track _Track = new Track();
  
      _Track.OnResponseEvent +=
          new Track.ResponseEventHandler(UpdateTrackResponseWindow);
  }
  </code></pre>
  
  <p>So, the idea is that every time I have a new message coming from the <code>OnResponseEvent</code> Thread, I run the <code>UpdateTrackResponseWindow()</code> method.  This method will append a new message string to an <code>ObservableCollection&lt;string&gt;</code> list property called <code>TrackResponseMessage</code>:</p>
  
  <pre><code>private void UpdateTrackResponseWindow(AbstractResponse message)
  {
      TrackResponseMessage.Add(FormatMessageResponseToString(message));
  }
  </code></pre>
  
  <p>The <code>FormatMessageResponseToString()</code> method merely compares the message with all pre-defined message types within the <code>Track</code>, and does some nifty string formatting.</p>
  
  <p>The main problem is:  <strong>The UI disappears when <code>TrackResponseMessage.Add()</code> is run</strong>. The executable is still running in the background, and the only way to end the task is to shut down Visual Studio 2010.  </p>
  
  <p><code>TrackResponseMessage</code> is a public property within my ViewModel:</p>
  
  <pre><code>public ObservableCollection&lt;String&gt; TrackResponseMessage
  {
      get { return _trackResponseMessage; }
      set
      {
          _trackResponseMessage = value;
          RaisePropertyChanged("TrackResponseMessage");
      }
  }
  </code></pre>
  
  <p>Is there a need for me to marshal the <code>Thread</code> coming from the <code>Track</code> object to my ViewModel? Any example code would be very appreciated!</p>