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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:34:22+00:00 2026-06-13T23:34:22+00:00

I’m trying async-await programming with entity-framework 6 (code first) + WPF and I can’t

  • 0

I’m trying async-await programming with entity-framework 6 (code first) + WPF and I can’t see why the UI still freezes after I made the code asynchronous.
Here is what I’m doing from the very first line:

first there is an event handler responding to a click button:

private async void LoginButton_Click(object sender, RoutedEventArgs e) {
  if (await this._service.Authenticate(username.Text, password.Password) != null)
    this.Close();
}

Then I have the Authenticate method in my service layer:

public async Task<User> Authenticate(string username, string password) {
  CurrentUser = await this._context.GetUserAsync(username.ToLower().Trim(), password.EncryptPassword());
  return CurrentUser;
}

and at the end is the EF code in the context:

public async Task<User> GetUserAsync(string username, string password) {
  return await this.People.AsNoTracking().OfType<User>().FirstOrDefaultAsync(u => u.Username == username && u.Password == password);
}

Update: After some tracing the cause of UI freezing turned out to be the initialization process. UI thread blocks until the EF context is initialized and once that is done the actual querying/saving process is performed asynchronously.

Update Debug output after the call on Task.Yield() at the beginning of the click handler:

53:36:378 Calling Task.Yield
53:36:399 Called Task.Yield
53:36:400 awaiting for AuthenticateAsync
53:36:403 awaiting for GetUserAsync
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data.OracleClient\v4.0_4.0.0.0__b77a5c561934e089\System.Data.OracleClient.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'D:\SkyDrive\Works\MyApp\MyApp.UI.WPF.Shell\bin\Debug\EntityFramework.SqlServer.dll'
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.Wrapper.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'EntityFrameworkDynamicProxies-MyApp.Model.Domain.People'
'MyApp.vshost.exe' (Managed (v4.0.30319)): Loaded 'EntityFrameworkDynamicProxies-MyApp.Model.Domain.Security'
53:39:965 Out of GetUserAsync
53:39:968 out of AuthenticateAsync
The thread '<No Name>' (0x1e98) has exited with code 0 (0x0).
The thread '<No Name>' (0x17d4) has exited with code 0 (0x0).
The thread '<No Name>' (0x175c) has exited with code 0 (0x0).
The thread '<No Name>' (0x220) has exited with code 0 (0x0).
The thread '<No Name>' (0x1dc8) has exited with code 0 (0x0).
The thread '<No Name>' (0x1af8) has exited with code 0 (0x0).
  • 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-13T23:34:24+00:00Added an answer on June 13, 2026 at 11:34 pm

    Methods marked ‘async’ are still synchronous up until the point when the first ‘await’ happens. Because of that, if any of what happens in that initial code takes too long (200ms or more I think is the guideline for WinRT, which seems reasonable), then you may want to force the code to return faster by inserting an await earlier.

    For instance, in your LoginButton_Click, you can insert a first line of ‘await Task.Yield()’ which will allow the call to return back to the UI thread faster.

    Now, with that change alone, the methods will all still run on the UI thread due to the async/await behavior. I still like making that change first because in many cases it’s what the user actually expects is happening (the ‘async’ modifier being kind of confusing in that regard), and it’s something you can do at the start of a handler without having to mess with things further down the stack.

    The next step we can do if the above is insufficient (like the context initialization is taking too long, still happens on the UI thread, and still freezes the UI, just at a slightly different point in time) is taking those parts that don’t need to happen on the UI thread and let await know that they can be processed on any thread, not just the UI thread. This is generally a good practice anyway for responsiveness, even in scenarios where the code currently runs ‘fast enough’ to not be a noticeable problem.

    For that, we use add ConfigureAwait(false) to the Task.

    • GetUserAsync method should add it (“chain” it) after the FirstOrDefaultAsync call
      • alternatively, and IMHO slightly cleaner, is to just get rid of the async/await keywords in the GetUserAsync method and just return the task you get back from FirstOrDefaultAsync. The async/await isn’t really ‘buying’ you anything in this method as-is, IMHO 🙂
    • in Authenticate, you should likely add it after the GetUserAsync call
      • the one potential ‘gotcha’ here that I’m not sure about is if the CurrentUser is data-bound into the UI. Since it’s a member of the _service, I’m guessing it’s not, but even if it is, I think that WPF is fine with data-bound items being updated on non-UI threads and it deals with marshaling the changes back to the UI (dispatcher?) thread. This is different than frameworks like Silverlight where updating a property on a non-UI that’s data-bound into the UI will cause the same cross-thread failure as if you had manually updated the target control. If I’m wrong about this, and 1) the CurrentUser is data-bound into your UI and 2) that data-bound update on a non-UI thread causes a runtime exception, then avoid the ConfigureAwait(false) addition in this method. Sorry for the length of this, just trying to relate what I’m a little unsure of this particular modification. 🙂
    • in LoginButton_Click, we should NOT add it, because the rest of the method (this.Close) needs to happen on the UI thread, and ConfigureAwait(false) here would break that

    Once both of those changes are in, you’ll both 1) be returning control back to the caller as quickly as you can (doing the least amount of code synchronous to the event handler) and 2) be doing work that doesn’t need to be on the UI thread on other threads, which should hopefully mean your UI doesn’t ‘freeze’ any more.

    If it still freezes after those changes, you might just need to run it under a debugger, and when it freezes, break to see what the stack of the UI thread is to find the offending code. 🙂

    Good luck!

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.