Click here to Skip to main content
15,905,238 members
Home / Discussions / C#
   

C#

 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
OriginalGriff30-Jan-21 1:58
mveOriginalGriff30-Jan-21 1:58 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
arnold_w30-Jan-21 2:02
arnold_w30-Jan-21 2:02 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
OriginalGriff30-Jan-21 2:39
mveOriginalGriff30-Jan-21 2:39 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
arnold_w30-Jan-21 2:50
arnold_w30-Jan-21 2:50 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
OriginalGriff30-Jan-21 5:45
mveOriginalGriff30-Jan-21 5:45 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
Gerry Schmitz30-Jan-21 3:04
mveGerry Schmitz30-Jan-21 3:04 
JokeRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
Peter_in_278030-Jan-21 10:39
professionalPeter_in_278030-Jan-21 10:39 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
Gerry Schmitz30-Jan-21 12:29
mveGerry Schmitz30-Jan-21 12:29 
AnswerRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
Ralf Meier29-Jan-21 23:25
mveRalf Meier29-Jan-21 23:25 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
OriginalGriff29-Jan-21 23:57
mveOriginalGriff29-Jan-21 23:57 
GeneralRe: What queuing technique and GUI component should I choose for my multi-comport, receive-only terminal software? Pin
arnold_w30-Jan-21 0:40
arnold_w30-Jan-21 0:40 
AnswerRe: multi-comport receiver Pin
Luc Pattyn31-Jan-21 4:35
sitebuilderLuc Pattyn31-Jan-21 4:35 
QuestionWrite Async Method Pin
Kevin Marois27-Jan-21 6:37
professionalKevin Marois27-Jan-21 6:37 
AnswerRe: Write Async Method Pin
OriginalGriff27-Jan-21 8:07
mveOriginalGriff27-Jan-21 8:07 
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 

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.