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 763937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:38:25+00:00 2026-05-14T16:38:25+00:00

In my code I subscribe to an event that happens on a different thread.

  • 0

In my code I subscribe to an event that happens on a different thread. Every time this event happens, I receive an string that is posted to the observable collection:

    Dispatcher currentDispatcher = Dispatcher.CurrentDispatcher;
    var SerialLog = new ObservableCollection<string>();

    private void hitStation_RawCommandSent(object sender, StringEventArgs e)
    {
        string command = e.Value.Replace("\r\n", "");
        Action dispatchAction = () => SerialLog.Add(command);
        currentDispatcher.BeginInvoke(dispatchAction, DispatcherPriority.Render);
    }

The code below is in my view model (could be in the code behind, it doesn’t matter in this case). When I call “hitstation.PrepareHit”, the event above gets called a couple times, then I wait and call “hitStation.HitBall”, and the event above gets called a couple more times.

    private void HitBall()
    {
        try
        {
            try
            {
                Mouse.OverrideCursor = Cursors.Wait;

                //prepare hit
                hitStation.PrepareHit(hitSpeed);

                Thread.Wait(1000);
                PlayWarning();

                //hit
                hitStation.HitBall(hitSpeed);
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show("Timeout hitting ball: " + ex.Message);
            }
        }
        finally
        {
            Mouse.OverrideCursor = null;
        }
    }

The problem I’m having is that the ListBox that is bound to my SerialLog gets updated only when the HitBall method finishes. I was expecting seeing a bunch of updates from the PrepareHit, a pause and then a bunch more updates from the HitBall.

I’ve tried a couple of DispatcherPriority arguments, but they don’t seem to have any effect.

  • 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-14T16:38:26+00:00Added an answer on May 14, 2026 at 4:38 pm

    I think you are blocking yourself.

    The UI Thread is waiting at Thread.Wait, BeginInvoke sends the action to the dipatcher, however the UI Thread is busy waiting. That is why UI updates only get done after HitBall finishes, that is, when the UI Thread finishes processing.

    To get around this, you should start the HitBall method’s code in another thread, freeing the UI:

     private void HitBall() 
     { 
         try {
    
             Mouse.OverrideCursor = Cursors.Wait;
             Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
    
             Action hitOperations = () =>
             {
                 hitStation.PrepareHit(hitSpeed);
    
                 Thread.Wait(1000);
    
                 //Only needed if PlayWarning needs access to UI Thread
                 dispatcher.Invoke(() => PlayWarning());
    
                 hitStation.HitBall(hitSpeed);
    
                 dispatcher.Invoke(() => Mouse.OverrideCursor = null);
              };
    
              //Free the UI from work, start operations in a background thread
              hitOperations.BeginInvoke(null, null);
    
          }
          catch (TimeoutException ex)
          {
              MessageBox.Show("Timeout hitting ball: " + ex.Message);
          }
     }
    

    Also if the intended use of Thread.Wait(1000) was to wait for events to refresh the UI, with this implementation it is no longer needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fake class that contains an event. My code should subscribe to
What's the code to subscribe to an event like applicationWillResignActive in any place in
I've got a user control that has an event that I can subscribe to.
I have this code in a .html.erb file: <script src=http://connect.facebook.net/en_US/all.js#xfbml=1></script> <script> FB.Event.subscribe('edge.create', function(response) {
I have used below code to attach quckboook events // Subscribe to UI events...
Code: String message = MessageFormat.format(error {0},e); E.g. message: java.text.ParseException: Unparseable date: sdf sf sa
Code: <?php //initializing script, do not modify session_start(); define('IN_SCRIPT', true); //so that global.php cannot
Code sample for my question: IList<TestDataAnimal> testDataFromDb = this.db.TestDataAnimals.Include(t => t.TestType).Include(t => t.Visit).Include(t =>
I've spent far too much time with this and can't find the mistake. Maybe
I am creating an event-based API where a user can subscribe to an event

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.