0

I have a class costructor:

public AllSignalsViewModel()
    {
        LoadSignals();
        fetchMoreDataCommand = new DelegateCommand(
            obj =>
                {
                    if (busy)
                    {
                        return;
                    }
                    Busy = true;
                    ThreadPool.QueueUserWorkItem(
                        delegate
                        {
                            Deployment.Current.Dispatcher.BeginInvoke(
                                delegate 
                                {
                                    AddMoreItems();
                                    Busy = false;

                                });
                        });                 
            });
    }

LoadSignals() is an async function, but I need to let it ends, before the running of the following code. But I can't await it, because I can't set the constructor as async. What I should to do?

Here is a LoadSignals() function:

private async void LoadSignals()
    {
        var xmlDocument = new XmlDataSource();
        var sigCol = new SignalCollection(xmlDocument);
        var region = GetRegionInSettingsStorage();
        var allSignals = await sigCol.LoadSignals(isVotes(), false, region);
        signalSchema = allSignals;
        AddMoreItems();

    }

What to do?

splash27
  • 2,057
  • 6
  • 26
  • 49

1 Answers1

0

I recommend implementing an InitializeAsync method which gets called in the code behind (or even in the constructor without await).

But LoadSignals must return task otherwise you cannot await it...

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
  • It will not solve my problem, because It impossible to make fetchMoreDataCommand = new DelegateCommand(...) outside the constructor. =( – splash27 May 30 '14 at 09:28
  • then you have to check inside of the command if the load signals has completed and wait for it if necessary... – Rico Suter May 30 '14 at 10:54