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

  • Home
  • SEARCH
  • 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 6604251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:07:46+00:00 2026-05-25T19:07:46+00:00

I have created a simple WPF Application and added a button to the default

  • 0

I have created a simple WPF Application and added a button to the default window. When I click on the button, a simulated long working method(simulated using a Thread.Sleep(15000) is called. I am trying to make the button execute asynchronously however despite following online examples, the button and entire window locks as soon as I click and remains so until the Thread.Sleep(…) finishes.

Any ideas why this is happening?

Here is the code:

private void button1_Click(object sender, RoutedEventArgs e)
{
   DoSomeAsyncWork();
}

private void DoSomeAsyncWork()
{
     System.Windows.Threading.Dispatcher.Run();
     Thread thread = new System.Threading.Thread(
         new System.Threading.ThreadStart(
          delegate()
          {
               Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => Thread.Sleep(15000)));
          }
        ));
     thread.Start();
}
  • 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-25T19:07:47+00:00Added an answer on May 25, 2026 at 7:07 pm

    You are putting the long operation back into the UI thread. Let me comment your example:

    Thread thread = new System.Threading.Thread( 
        new System.Threading.ThreadStart( 
            delegate() { 
                // here we are in the background thread
    
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                    new Action(() => {
                        // here we are back in the UI thread
                        Thread.Sleep(15000);
                    })); 
          } 
        )); 
    

    So, you should modify your example like this:

    Thread thread = new System.Threading.Thread( 
        new System.Threading.ThreadStart( 
            delegate() { 
                // here we are in the background thread
    
                Thread.Sleep(15000);  // <-- do the long operation here
    
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, 
                    new Action(() => {
                        // here we are back in the UI thread
    
                        // do stuff here that needs to update the UI after the operation finished
                    })); 
          } 
        )); 
    

    As others have mentioned, it’s easier to use the BackgroundWorker class. Here’s an example:

    private void DoSomeAsyncWork()   
    {   
        BackgroundWorker bw = new BackgroundWorker();
    
        bw.DoWork += (sender, args) => {
            // do your lengthy stuff here -- this will happen in a separate thread
            Thread.Sleep(15000);
        }
    
        bw.RunWorkerCompleted += (sender, args) => {
            if (args.Error != null)  // if an exception occurred during DoWork,
                MessageBox.Show(args.Error.ToString());  // do your error handling here
    
            // do any UI stuff after the long operation here
            ...
        }
    
        bw.RunWorkerAsync(); // start the background worker
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple WPF application in which I am using data binding
I have created a simple (commercial) WPF application and want to distribute it with
Could someone help me on this, I have created simple web services using axis2
I have created some simple app in Java, and 'deployed' it using Java Web
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
Greetings all, I have a question. I have created a WPF application. So, I
I have a WPF application and I added to the project resources many icons
I have created a TabControl in a WPF application I'm writing. I re-templated TabItem
It is about WPF application which generates reports. Reports have simple structure: byte[] m_Data,
I've created a simple desktop application in C# 3.0 to learn some C#, wpf

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.