Click here to Skip to main content
15,310,056 members
Home / Discussions / C#
   

C#

 
AnswerRe: causing the computer to freeze Pin
Gerry Schmitz2-Jan-22 4:42
mveGerry Schmitz2-Jan-22 4:42 
GeneralRe: causing the computer to freeze Pin
CalinNegru3-Jan-22 8:11
MemberCalinNegru3-Jan-22 8:11 
AnswerRe: causing the computer to freeze Pin
OriginalGriff2-Jan-22 6:29
mveOriginalGriff2-Jan-22 6:29 
GeneralRe: causing the computer to freeze Pin
CalinNegru3-Jan-22 6:13
MemberCalinNegru3-Jan-22 6:13 
GeneralRe: causing the computer to freeze Pin
OriginalGriff3-Jan-22 6:45
mveOriginalGriff3-Jan-22 6:45 
AnswerRe: causing the computer to freeze Pin
#realJSOP2-Jan-22 23:53
mva#realJSOP2-Jan-22 23:53 
AnswerRe: causing the computer to freeze Pin
0x01AA5-Jan-22 4:40
professional0x01AA5-Jan-22 4:40 
QuestionAsync/Await Questions Pin
Kevin Marois30-Dec-21 7:35
professionalKevin Marois30-Dec-21 7:35 
I see a lot of Async/Await examples where an async method returns Task, but I'm confused on some things.

Let's says I create a class to do some processing:
public class Processor
{
    public async Task Process()
    {
        await DoSomething();
    }

    private async Task DoSomething()
    {
        var customers = await GetSomeData();

        // Do some work on the customer data

        await SaveResults(customers);
    }

    private async Task> GetSomeData()
    {
        List customers = null;

        await Task.Run(() =>
        {
            customers = Repository.GetCustomers();
        });
    }

    private async Task SaveResults(List customers)
    {
        await Task.Run(() =>
        {
            Repository.SaveCustomers(customers);
        });
    }
}
I then want to start the processing from a button click in a ViewModel:
public class MainWindowViewModel : _ViewModelBase
{
    private ICommand _ProcessCommand;
    public ICommand ProcessCommand
    {
        get
        {
            if (_ProcessCommand == null)
                _ProcessCommand = new RelayCommand(p => ProcessExecuted(), p => ProcessCanExecute());
            return _ProcessCommand;
        }
    }

    private bool ProcessCanExecute()
    {
        return true;
    }

    private void ProcessExecuted()
    {
        Processor p = new Processor();
        p.Process();
    }
}
First Question
In the ViewModel's ProcessExecuted, the call:
p.Proccess
shows a compiler warning
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
This method is from a ICommand, so do I really want to await this?

Second Question
Next, in the process class, all of the methods are marked as async because if I don't, then I get the same warning. It seems that you have to make every call async along the entire call stack. What I'm hoping to do is offload the Load/Save calls and let the UI keep going.

What is the right way to do this?

Third Question
When my window loads I want to get data for some comboboxes. Is this the right way to do this?
private async Task LoadLists()
{
    var statuses = new List<Status>();
    var states = new List<State>();
    var employees = new List<Employee>();

    var loadDataTasks = new Task[]
    {
        Task.Run(async () => statuses = await LoadStatuses()),
        Task.Run(async () => states = await LoadStates()),
        Task.Run(async () => employees = await LoadEmployees())
    };

    try
    {
        await Task.WhenAll(loadDataTasks);
    }
    catch (Exception ex)
    {
        // handle exception
    }
    finally
    {
        // Populate ViewModel properties
        Statuses = new List(statuses);
        States = new List(states);
        Employees = new List(employees);
    }
}

private async Task> LoadStatuses()
{
    return Repository.GetStatuses();
}

private async Task> LoadStates()
{
    return Repository.GetStates();
}

private async Task> LoadEmployees()
{
    return Repository.GetEmployees();
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Async/Await Questions Pin
Gerry Schmitz30-Dec-21 18:43
mveGerry Schmitz30-Dec-21 18:43 
QuestionRead signature data from APK's RSA signature file Pin
mynametaken30-Dec-21 4:46
Membermynametaken30-Dec-21 4:46 
AnswerRe: Read signature data from APK's RSA signature file Pin
jschell30-Dec-21 7:35
Memberjschell30-Dec-21 7:35 
GeneralRe: Read signature data from APK's RSA signature file Pin
mynametaken1-Jan-22 5:53
Membermynametaken1-Jan-22 5:53 
AnswerRe: Read signature data from APK's RSA signature file Pin
Luc Pattyn30-Dec-21 7:44
sitebuilderLuc Pattyn30-Dec-21 7:44 
GeneralRe: Read signature data from APK's RSA signature file Pin
mynametaken1-Jan-22 5:56
Membermynametaken1-Jan-22 5:56 
GeneralRe: Read signature data from APK's RSA signature file Pin
Luc Pattyn1-Jan-22 5:59
sitebuilderLuc Pattyn1-Jan-22 5:59 
GeneralRe: Read signature data from APK's RSA signature file Pin
mynametaken9-Jan-22 4:37
Membermynametaken9-Jan-22 4:37 
AnswerRe: Read signature data from APK's RSA signature file Pin
jschell23-Jan-22 6:49
Memberjschell23-Jan-22 6:49 
QuestionWinform support in Windows service Pin
Member 1260803929-Dec-21 23:59
MemberMember 1260803929-Dec-21 23:59 
AnswerRe: Winform support in Windows service Pin
OriginalGriff30-Dec-21 0:27
mveOriginalGriff30-Dec-21 0:27 
GeneralRe: Winform support in Windows service Pin
Member 1260803930-Dec-21 1:28
MemberMember 1260803930-Dec-21 1:28 
GeneralRe: Winform support in Windows service Pin
OriginalGriff30-Dec-21 2:08
mveOriginalGriff30-Dec-21 2:08 
AnswerRe: Winform support in Windows service Pin
#realJSOP30-Dec-21 1:20
mva#realJSOP30-Dec-21 1:20 
QuestionDynamically change int from a string Pin
Matte Matik29-Dec-21 12:02
MemberMatte Matik29-Dec-21 12:02 
AnswerRe: Dynamically change int from a string Pin
Dave Kreskowiak29-Dec-21 12:36
mveDave Kreskowiak29-Dec-21 12:36 
AnswerRe: Dynamically change int from a string Pin
OriginalGriff29-Dec-21 21:53
mveOriginalGriff29-Dec-21 21:53 

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.