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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:01:42+00:00 2026-05-28T01:01:42+00:00

I am working on a WPF application. I have a time consuming method that

  • 0

I am working on a WPF application. I have a time consuming method that I want to run async via BackgroundWorker. While the method runs, I want to display a modal “Please Wait…” dialog window, which must automatically close when the BackgroundWorker completes.

I currently have very little experience with BackgroundWorker or any multi threaded programming.

The code below currently results in an InvalidOperationException, with the message “The calling thread must be STA, because many UI components require this.”

Please advise me on how to achieve what I am trying to achieve, and extra brownie-points if you can help me understand what is going wrong.

Many thanks!

EDIT
Just to clarify – The idea is that the main thread launches the BackgroundWorker, then shows the modal dialog. When the worker completes, it closes the modal dialog. When the modal dialog closes, the main thread continues.

public class ImageResizer
{
    private BackgroundWorker worker;
    private MemoryStream ImageData { get; set; } // incoming data
    private public MemoryStream ResizedImageData { get; private set; } // resulting data
    private Dialogs.WorkInProgressDialog ProgressDialog;


    // Public interface, called by using class:
    public MemoryStream ReduceImageSize(MemoryStream imageData)
    {
        // injected data:
        this.ImageData = imageData;

        // init progress dialog window:
        ProgressDialog = new Dialogs.WorkInProgressDialog();

        // Start background worker that asyncronously does work
        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);            
        worker.RunWorkerAsync();

        // Show progress dialog. Dialog is MODAL, and must only be closed when resizing is complete
        ProgressDialog.ShowDialog(); // THIS LINE CAUSES THE INVALID OPERATION EXCEPTION

        // This thread will only continue when ProgressDialog is closed.

        // Return result
        return ResizedImageData;
    }

    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Call time consuming method
        ResizedImageData = ReduceImageSize_ActualWork();
    }

    // The actual work method, called by worker_DoWork
    private MemoryStream ReduceImageSize_ActualWork()
    {
        // Lots of code that resizes this.ImageData and assigns it to this.ResizedImageData
    }

    private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {   
        // Async work completed - close progress dialog
        ProgressDialog.Close();
    }
}
  • 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-28T01:01:42+00:00Added an answer on May 28, 2026 at 1:01 am

    You can’t call ShowDialog from the BackgroundWorker. You have to use the Dispatcher to ask the UI thread to execute it:

     this.Dispatcher.BeginInvoke(new Action(() => ProgressDialog.ShowDialog()));
    

    The ‘Completed’ event of the BackgroundWorker is executed in the UI thread, so this part should be fine.

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

Sidebar

Related Questions

I have a project I'm working on that requires our WPF application read SMS
I'm building a WPF application and working with the MVVM pattern. I have 4
I have created some tests in my WPF application. Right now I am working
I'm currently working out the layout of a WPF Application and seem to have
I am working in C# and VS2008. I have a WPF application containing a
I'm working on a WPF application that sometimes exhibits odd problems and appears to
I am working on a WPF application which has a treeview that represents an
I was working with a WPF application and I decided that the controls and
In my application i have a WPF Window that has a DataGrid in it.
I am working on a wpf application and i have to implement inline editing

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.