Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:12:38+00:00 2026-05-26T00:12:38+00:00

I am converting a chat parser for a game i play that i wrote

  • 0

I am converting a chat parser for a game i play that i wrote in c# winforms over to wpf, mainly just to get a better handle on MVVM and wpf. Here is a run down of how i have my project set up

View:
For now its just a simple ListBox with ItemSource bound to my viewmodels observable chat collection

Model:
I have multiple characters that can be logged in at one time and each character has a chat class. The chat class starts a background worker that grabs and next line of chat from the game and fires off an event called IncomingChat with this line.

public event Action<Game.ChatLine> IncomingChat;

I’m using a background worker to fire an event in my backgroundworkers progresschaged event because when i was using a timer i kept getting a threading issue. At first i corrected this by changing my Timer to a DispatchTimer, but this didn’t seem right to me to have a DispatchTimer in my model.

ViewModel:
Since i have multiple characters i am creating multiple ChatViewModels. I pass a character into the ChatViewModels constructor and subscribe to the Chat event. I create a ObservableColleciton to hold my chat lines when this event is received. Now I’m receiving a threading issue on my viewModel when trying to add the line i receive from my chat event to my observablecollection.

I got around this by making my viewmodels incoming chat event handler look like so

public ObservableCollection<Game.ChatLine) Chat {get; private set;}

void Chat_Incoming(Game.ChatLine line)
{
  App.Current.Dispatcher.Invoke(new Action(delegate
  {
    Chat.Add(line)
  }), null);
}

This doesn’t feel right to me though. Although it works, using Dispatcher in my viewmodel like this seems out of place to me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:12:38+00:00Added an answer on May 26, 2026 at 12:12 am

    Although it works, using Dispatcher in my viewmodel like this seems out of place to me.

    This isn’t a completely unreasonable approach, and is the approach that many people take. Personally, if you’re using WPF (or Silverlight 5), and have access to the TPL, I prefer to use the TPL to handle this.

    Assuming your ViewModel is constructed on the UI thread (ie: by the View, or in response to a View related event), which is the case nearly always IMO, you can add this to your constructor:

    // Add to class:
    TaskFactory uiFactory;
    
    public MyViewModel()
    {
        // Construct a TaskFactory that uses the UI thread's context
        uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
    }
    

    Then, when you get your event, you can use this to marshal it:

    void Chat_Incoming(Game.ChatLine line)
    {
        uiFactory.StartNew( () => Chat.Add(line) );
    }
    

    Note that this is slightly different than your original, since it’s no longer blocking (this is more like using BeginInvoke instead of Invoke). If you need this to block until the UI finishes processing the message, you can use:

    void Chat_Incoming(Game.ChatLine line)
    {
        uiFactory.StartNew( () => Chat.Add(line) ).Wait();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am considering converting a project that I've inherited from .net 1.1 to .net
I'm converting an old app that records folder sizes on a daily basis. The
How does C handle converting between integers and characters? Say you've declared an integer
I am an old Unix guy who is converting a makefile based project over
Converting dates/times into ticks using the PowerShell Get-Date applet is simple. However, how do
While converting a project that used SlimDX, and therefore has unmanaged code, to .NET
When converting from RGB to grayscale, it is said that specific weights to channels
IN converting over a legacy application we need to convert named query to nhibernate.
Just converting some shell scripts into batch files and there is one thing I
We need to create a Server with Scala RemotActors, that can handle multiple clients.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.