Note that there are some explanatory texts on larger screens.

plurals
  1. POasync-await issue in EF 6
    primarykey
    data
    text
    <p>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:</p> <p>first there is an event handler responding to a click button:</p> <pre><code>private async void LoginButton_Click(object sender, RoutedEventArgs e) { if (await this._service.Authenticate(username.Text, password.Password) != null) this.Close(); } </code></pre> <p>Then I have the Authenticate method in my service layer:</p> <pre><code>public async Task&lt;User&gt; Authenticate(string username, string password) { CurrentUser = await this._context.GetUserAsync(username.ToLower().Trim(), password.EncryptPassword()); return CurrentUser; } </code></pre> <p>and at the end is the EF code in the context:</p> <pre><code>public async Task&lt;User&gt; GetUserAsync(string username, string password) { return await this.People.AsNoTracking().OfType&lt;User&gt;().FirstOrDefaultAsync(u =&gt; u.Username == username &amp;&amp; u.Password == password); } </code></pre> <p><strong>Update:</strong> 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. </p> <p><strong>Update</strong> Debug output after the call on Task.Yield() at the beginning of the click handler:</p> <pre><code>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 '&lt;No Name&gt;' (0x1e98) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x17d4) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x175c) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x220) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x1dc8) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x1af8) has exited with code 0 (0x0). </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload