Link to home
Start Free TrialLog in
Avatar of thready
thready

asked on

C# async & await, WPF application

Hi Experts,

I have a function that's setting the DataContext of a UserControl the following way:

m_MyBusinessObject = CreateMyBusinessObject();
base.DataContext = new MyViewModel( m_MyBusinessObject );

Open in new window


There are async & await calls within the CreateMyBusinessObject, so when I get to the DataContext assignment, there's nothing in the data fields of m_MyBusinessObject yet that the MyViewModel will build from, so the UserControl is created empty.

How do I wait for m_MyBusinessObject to be "filled in" properly by the await functions before passing it to the new MyViewModel without freezing up the UI?  The CreateMyBusinessObject call can take a very long time.

Thanks,
Mike
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thready
thready

ASKER

Of course!  Thank you  :)
Avatar of thready

ASKER

Actually, I thought I understood, but I guess I don't.

If I create a background worker, how does that help me?  I've got the same problem.  I need to wait for that background worker to complete before setting my DataContext.  If I add the code above to a worker thread, sure my UI won't freeze up while it's creating the business object.  But when the thread has created the business object and then polls and waits for completion, once it completes, the worker thread then tries to set the DataContext and bam- exception because I'm on the wrong thread.
If I understand correctly the core issue here is the length of time to create the BusinessObject class. Based on this I am suggesting you use backgroundworker to create and return this BusinessObject class and then continue processing as normal. To do this use the BackgroundWorker argument DoWorkEventArgs with its public property object Result. When your worker has generated its result  set e.Result and return.

eg

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
    var mbo = CreateMyBusinessObject();
    while (m_MyBusinessObject.created != True)
    {
        System.Threading.Thread.Sleep(500);
    }
    e.Result = mbo ;
}

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    m_MyBusinessObject  = e.Result;
}

Open in new window

Avatar of thready

ASKER

I guess my real issue is, I have to add to my view model item by item (it's a treeview), instead of all in one shot like I'm doing after the whole tree gets created in the model.
The suggestion above is just to replace the line

m_MyBusinessObject = CreateMyBusinessObject();

so that the m_MyBusinessObject  object creation is fully completed before creating a new ViewModel and so ensuring that this is not created empty.
Avatar of thready

ASKER

Thank you again for your help!  Much appreciated.