Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
AnswerRe: Write Async Method Pin
Richard Deeming27-Jan-21 21:46
mveRichard Deeming27-Jan-21 21:46 
GeneralRe: Write Async Method Pin
Kevin Marois28-Jan-21 6:27
professionalKevin Marois28-Jan-21 6:27 
GeneralRe: Write Async Method Pin
Kevin Marois28-Jan-21 7:53
professionalKevin Marois28-Jan-21 7:53 
GeneralRe: Write Async Method Pin
Richard Deeming28-Jan-21 22:10
mveRichard Deeming28-Jan-21 22:10 
GeneralRe: Write Async Method Pin
Kevin Marois29-Jan-21 9:12
professionalKevin Marois29-Jan-21 9:12 
GeneralRe: Write Async Method Pin
Richard Deeming31-Jan-21 22:05
mveRichard Deeming31-Jan-21 22:05 
GeneralRe: Write Async Method Pin
Kevin Marois31-Jan-21 22:36
professionalKevin Marois31-Jan-21 22:36 
GeneralRe: Write Async Method Pin
Richard Deeming1-Feb-21 0:02
mveRichard Deeming1-Feb-21 0:02 
Again, there are two parts to this.

On the server, ExecuteScalarAsync returns a Task, which uses an IO completion port behind the scenes to complete the task when the response has been received from the database. This allows the server to reuse the same thread-pool thread to service other requests whilst it waits for your database query to finish.

The client doesn't care what the server is doing. All it cares about is sending a request and receiving a response. But again, the network library can use an IO completion port behind the scenes so that your thread isn't tied up waiting for a response from the server. It doesn't matter to the client whether the server has implemented the API using Tasks, or using a synchronous method; the network communication is the target for the asynchronous code.

I think at least part of the confusion here stems from the fact that you're using the same interface for the server implementation of the API and the client code to call that API. It's perfectly valid to have an async method on the client calling a synchronous method on the server.

With Linq2Sql, it's not simple to make your server method async *. But that doesn't mean the client can't be async.
C#
protected override void Load()
{
    _ = LoadAsync();
}

private async Task LoadAsync()
{
    var customer = await AppCore.BizObject.GetCustomerByIdAsync(id);
    ...
}
Again, take note of the fact that you should avoid async void methods, and try to avoid spinning up a new Task just to push work off the UI thread.


* You'd have to manually convert the query to a SqlCommand and execute it by hand. Mike Taulty made a start on this back in 2007, before async/await was around:
LINQ to SQL: Asynchronously Executing Queries | Mike Taulty[^]

Perhaps try something like this:
C#
public async Task<CompanyEntity> GetCompanyByIdAsync(int companyId)
{
    using (var dc = GetDataContext())
    {
        var query = (from c in dc.Companies
                     where c.Id == companyId
                     select new CompanyEntity
                     {
                         Id = c.Id,
                         CompanyName = c.CompanyName
                     });

        using (var command = (SqlCommand)dc.GetCommand(query))
        using (var reader = await command.ExecuteReaderAsync())
        {
            return dc.Translate<CompanyEntity>(reader).FirstOrDefault();
        }
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Write Async Method Pin
Kevin Marois1-Feb-21 8:36
professionalKevin Marois1-Feb-21 8:36 
GeneralRe: Write Async Method Pin
Richard Deeming1-Feb-21 21:20
mveRichard Deeming1-Feb-21 21:20 
QuestionHow to update members of a List using Linq? [Solved] Pin
Alex Dunlop26-Jan-21 23:54
Alex Dunlop26-Jan-21 23:54 
AnswerRe: How to update members of a List using Linq? Pin
OriginalGriff27-Jan-21 0:15
mveOriginalGriff27-Jan-21 0:15 
GeneralRe: How to update members of a List using Linq? Pin
Alex Dunlop27-Jan-21 2:15
Alex Dunlop27-Jan-21 2:15 
GeneralRe: How to update members of a List using Linq? Pin
OriginalGriff27-Jan-21 2:36
mveOriginalGriff27-Jan-21 2:36 
GeneralRe: How to update members of a List using Linq? Pin
Alex Dunlop27-Jan-21 3:04
Alex Dunlop27-Jan-21 3:04 
GeneralRe: How to update members of a List using Linq? Pin
Richard Deeming27-Jan-21 4:10
mveRichard Deeming27-Jan-21 4:10 
GeneralRe: How to update members of a List using Linq? Pin
Dave Kreskowiak27-Jan-21 7:06
mveDave Kreskowiak27-Jan-21 7:06 
QuestionProper event handling in docked window schema Pin
gizbernus26-Jan-21 9:16
gizbernus26-Jan-21 9:16 
Questionalias names for Column in Linq Pin
simpledeveloper25-Jan-21 8:28
simpledeveloper25-Jan-21 8:28 
AnswerRe: alias names for Column in Linq Pin
Gerry Schmitz25-Jan-21 10:18
mveGerry Schmitz25-Jan-21 10:18 
AnswerRe: alias names for Column in Linq Pin
Richard Deeming25-Jan-21 21:38
mveRichard Deeming25-Jan-21 21:38 
Questionhow to print POS bills with a thermal printer Pin
Meax24-Jan-21 8:53
Meax24-Jan-21 8:53 
AnswerRe: how to print POS bills with a thermal printer Pin
Daniel Pfeffer24-Jan-21 9:23
professionalDaniel Pfeffer24-Jan-21 9:23 
GeneralRe: how to print POS bills with a thermal printer Pin
OriginalGriff24-Jan-21 10:25
mveOriginalGriff24-Jan-21 10:25 
AnswerRe: how to print POS bills with a thermal printer Pin
OriginalGriff24-Jan-21 10:26
mveOriginalGriff24-Jan-21 10:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.