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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:26:42+00:00 2026-06-13T10:26:42+00:00

i have the following code: public partial class MainWindow : Window { public MainWindow()

  • 0

i have the following code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.Background = Brushes.Red;
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        int res = WaitFiveSecMethod();
        this.text2.Text = res.ToString();
    }

    private int WaitFiveSecMethod()
    {
        Thread.Sleep(5000);
        return 5;
    }

    private async void Button_Click_3(object sender, RoutedEventArgs e)
    {
        var res = await WaitFiveSecMethodAsync();
        this.text1.Text = res;

    }

    private async Task<string> WaitFiveSecMethodAsync()
    {
        Thread.Sleep(5000);
        return "5";
    }

}

I want to show the difference between clicking the “sync” button (button 2) and clicking the “async” button (button 3). clicking both buttons should do the same time-consuming calculations (in my case represented with “Thread.Sleep(5000)”).
Currently using async and wait make my client stuck i thought these keywords shouldn’t block my client thread.
what am i doing wrong?

  • 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-13T10:26:44+00:00Added an answer on June 13, 2026 at 10:26 am

    Your code below works synchronously and block UI, not async:

    private async Task<string> WaitFiveSecMethodAsync()
    {
        Thread.Sleep(5000);
        return "5";
    }
    

    Remember async has just responsibility to enable await keyword, if you don’t have await, async is meaningless and turn to synchronous. So, async method must have at least one await keyword inside.

    The better way is, you should delay by awaiting Task.Delay which does not block your UI thread:

    private async Task<string> WaitFiveSecMethodAsync() 
    { 
        await Task.Delay(5000); 
        return "5"; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code public partial class MainWindow : Window { public MainWindow() {
I have the following code: public partial class queryTerm : System.Web.UI.UserControl { private static
I have the following code: public partial class NewWindow: Window { public static readonly
I have following code and it is working fine. public partial class MainWindow :
I have the following code : public partial class ModificarAlimento : Form { private
I have the following code: public partial class Main : Form { private delegate
I have following code public class TEST { public static void main(String arg[]){ try
I have the following code: public partial class Form1 : Form { public BindingList<Class>
I have the following Silverlight code: [ScriptableType] public partial class Page : UserControl {
I have following class as the main window: public partial class Window1 : Window

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.