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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:12:41+00:00 2026-06-12T08:12:41+00:00

I’m trying to animate a loading screen while thread is working in background, but

  • 0

I’m trying to animate a loading screen while thread is working in background, but it won’t work.

I realized that it has something to do with the backgroundworker calling Button.Dispatcher, however I can’t remove it, because it would cause threading error. What should I do to fix this?

below is my code:

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            Storyboard sb = (Storyboard)FindResource("Loading");
            sb.Dispatcher.BeginInvoke(new Action(() => sb.Begin()), DispatcherPriority.Normal, null);

            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
            bg.RunWorkerAsync();
        }

        private void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(RunThread));
            thread.Start();            
        }

        private void RunThread()
        {
            btnLogin.Dispatcher.BeginInvoke(new Action(() => TheBackgroundWork()), DispatcherPriority.Normal, null);
        }

Update:

I remove UI calls from Do_Worker as suggested by Jay. Unfortunately I’m still getting an error:

The calling thread cannot access this object because a different thread owns it.

It was thrown by my webservice client

below is the updated code:

        private WebServiceClient proxy = new WebServiceClient();

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            sb = (Storyboard)FindResource("sbLoading");
            sb.Dispatcher.BeginInvoke(new Action(() => sb.Begin()), DispatcherPriority.Normal, null);

            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
            bg.RunWorkerAsync();
        }

        private void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var u = e.Result;
            if (u != null)
                //pass the data
        }

        private void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var u = proxy.ToWebService(x,y); //this guy throws an error
                e.Result = u;
            }
            catch
            {
                this.Dispatcher.Invoke(new Action(
                    () => MessageBox.Show("Connection Error", this.Title)), 
                    DispatcherPriority.Normal, null);
            }
        }
  • 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-06-12T08:12:42+00:00Added an answer on June 12, 2026 at 8:12 am
    1. Your bg_DoWork method will already be running on a background thread, so no need to create a thread in there.
    2. Nothing in bg_DoWork should touch UI elements nor anything databound to UI elements. If you have some return data, package it up into an object and stuff it into the DoWorkEventArgs.Result property.
    3. Add another handler for the BackgroundWorker.RunWorkerCompleted event. This method will run back on the UI thread, so you can read your response data from the RunWorkerCompletedEventArgs (this will be the result object that you assigned in #2) and update the UI as necessary.

    The beauty of BackgroundWorker is that it will handle thread creation and marshaling of data back to the UI thread — no need for any Dispatcher.Invoke.

    Update

    In response to your edit, the web service call may be throwing an exception, but the thread exception you get is caused by trying to access this from a background thread.

    Just as you’d have to package your success result in DoWorkEventArgs.Result, you’d need to package a failure result there too.

    The object you pass back can be anything, so you can add things like a bool Success property, an Exception CaughtException property, etc.

    You’ll check these values in your RunWorkerCompleted handler and that is where you would show your MessageBox.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.