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