Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
GeneralRe: Write Async Method Pin
Kevin Marois27-Jan-21 8:37
professionalKevin Marois27-Jan-21 8:37 
GeneralRe: Write Async Method Pin
Victor Nijegorodov27-Jan-21 11:16
Victor Nijegorodov27-Jan-21 11:16 
GeneralRe: Write Async Method Pin
Pete O'Hanlon27-Jan-21 21:28
mvePete O'Hanlon27-Jan-21 21:28 
GeneralRe: Write Async Method Pin
Victor Nijegorodov27-Jan-21 22:35
Victor Nijegorodov27-Jan-21 22:35 
GeneralRe: Write Async Method Pin
Gerry Schmitz28-Jan-21 14:57
mveGerry Schmitz28-Jan-21 14:57 
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 
Ok, so I've always been confused by the Client/Server design in an async app. Is this the right way to make a server method async?
Server Side
public interface IServerAPI
{
    Task AddCompanyAsync(CompanyEntity entity);

    Task UpdateCompanyAsync(CompanyEntity entity);
}

public class ServerAPI : IServerAPI
{
    public async Task AddCompanyAsync(CompanyEntity entity)
    {
        var t = await Task.Factory.StartNew(() =>
        {
            // Add the company and return the new PK
            return 1;

        });

        return t;
    }

    public async Task UpdateCompanyAsync(CompanyEntity entity)
    {
        await Task.Factory.StartNew(() =>
        {
            // Update the company

        });
    }
}
WPF Client
Some code ommitted. As you saw, the Save command is in a base class, so I can't replace it with an AsyncCommand. Instead I have it calling an async SaveChanges method:
public partial class MainWindow : Window, INotifyPropertyChanged
{
    #region Base Class Abstract Code
    // This command is really in abase class. It calls an abstract method called SaveChanges, which I 
    // could modify to be async and call the SaveChanges with await, as I did here: 
    private ICommand _SaveCommand;
    public ICommand SaveCommand
    {
        get
        {
            if (_SaveCommand == null)
                _SaveCommand = new RelayCommand(p => SaveExecuted(), p => SaveCanExecute());
            return _SaveCommand;
        }
    }

    private bool SaveCanExecute()
    {
        return true;
    }

    private async void SaveExecuted()
    {
        try
        {
            await SaveChanges();
        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            // Do post-Save stuff here
        }
    }
    #endregion Base Class Abstract Code

    private async Task SaveChanges()
    {
        IServerAPI serverAPI = new ServerAPI();

        if (Company.Id == 0)
        {
            var newId = await serverAPI.AddCompanyAsync(Company);
            Company.Id = newId;
        }
        else
        {
            await serverAPI.UpdateCompanyAsync(Company);
        }
    }
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

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 
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 

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.