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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:47:11+00:00 2026-05-30T04:47:11+00:00

I have inherited some code that queries a DB over a WCF service and

  • 0

I have inherited some code that queries a DB over a WCF service and then employs a callback when it’s done. I am trying to add some code to that callback to update the UI as the data is processed. I’m finding that I cannot get the UI to update during that callback:

client.GetDataAsync();
client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(GetDataCompleted);

void GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
  // Loop through the data
  // ...
  textBlock1.Text= "test1";
  Dispatcher.BeginInvoke(() => textBlock1.Text= "test2" );
  var thread = new Thread(() =>
  {
     // textBlock1.Text= "test3"; (this throws a cross-thread access exception)
     Dispatcher.BeginInvoke(() =>
     {
       textBlock1.Text= "test4";
     });
  }
  thread.Start();
  // ...
  Debug.WriteLine("done");
}

None of these things update the UI until (apparently) the entire callback is completed. This post:

What thread calls the completed event handler on silverlight WCF calls?

suggests that the callback is running on the main UI thread so that the BeginInvoke call should be unnecessary. Even if I add various delays in the above code, it still doesn’t work. Is this possible? Is there a better way to do this?

(This is a follow-up question to this: Multiple asynchronous UI updates in Silverlight)

  • 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-30T04:47:12+00:00Added an answer on May 30, 2026 at 4:47 am

    degorolls is right in suggesting the TPL, your code would look like below (except without the comments)(Also, exceptions MUST be handled in the TPL, so that might make it not worth it, but I dont think it should).
    The first methods would remain the same, and yes in event-based async programming thread-safety is taken care of (ie: you always return to the same thread you called out from)
    I also noticed that the text output is all doing = instead of +=, but that is probably more of a problem of typing into overflow
    So, test1 and test2 will print out at the same time, however everything being spit out from the TPL code should print as it comes in.
    UI code should not be doing anything that requires too much time, though…only updating the UI. So, do think of this as a point to refactor?
    Let me know if this helps or if I missed what you were looking for.

    client.GetDataAsync();
    client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(GetDataCompleted);
    
    void GetDataCompleted(object sender, GetDataCompletedEventArgs e)
    {
      // Loop through the data
      // ...
      textBlock1.Text= "test1";
      //////Dispatcher should not be needed here as this IS on the main UI thread
      Dispatcher.BeginInvoke(() => textBlock1.Text= "test2" );
      //////Everything that happens here should NOT be on the main UI thread, thus the cross-thread access exception
      //////You can do Dispatcher.CheckAccess to determine if you need to invoke or not
      //////Notice the newCopyOfDataToBeWritten. This is a closure, 
      //////so using the same referenced object will result in errant data as it loops
      //////Also, doing it this way does not guarantee any order that this will be written out
      //////This will utilize the parallel fully, but there are ways to force the order
      var task = Task.Factory.StartNew(()=>
        {
          Dispatcher.BeginInvoke(()=>textBlock1.Text += newCopyOfDataToBeWritten)
        }
      );
      // ...
      ///////I assume this is the end of the loop?
      Debug.WriteLine("done");
    }
    

    ….
    the below dummied-down code based on what you posted seems to work for me

     var outsideThread = new Thread(()=>
     {         
         for(int i = 0; i < 20; i++)
              {
                  //This code will show all at once since it is on the main thread, 
                  //which is still running
                  //If you want this to display one at a time also, then you need
                  //to use threads and callbacks like below, also
                  Dispatcher.BeginInvoke(()=>{textBlock1.Text += "outer" + i;});
                  int newI = i;
                  var thread = new Thread(() =>
                  {
                      System.Threading.Thread.Sleep(1000 * newI);
                      Dispatcher.BeginInvoke(() =>
                      {
                          //This will display as it comes in
    
                          textBlock1.Text += "inner" + newI;
                      });
                  });
                  thread.Start();
              }
    });
    outsideThread.Start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited some verbose, repetitious code that I am trying to refactor. The
I have inherited some code which involves a scheduled task that writes data (obtained
I have inherited some really old VC6.0 code that I am upgrading to VS2008
I have inherited a bit of Java code that uses Hibernate. Some of the
I have inherited some code that uses the ref keyword extensively and unnecessarily. The
I have inherited some code that is not working as I think it should:
I have some inherited code that I am modifying. However, I am seeing something
I have just inherited some code and part of that code is to show
I have some inherited code that uses hibernate. I'm getting the following error: Caused
I have some inherited JS code that uses this format: function main(param) { var

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.