Solved

C# async & await, WPF application

Posted on 2015-02-01
7
134 Views
Last Modified: 2015-02-01
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
0
Comment
Question by:thready
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40582904
To stop the GUI from freezing during the operation use a backgroundworker
https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

With the CreateMyBusinessObject call why not place some logic in the class which sets a variable once the operation is complete. You could use something like

m_MyBusinessObject = CreateMyBusinessObject();
while (m_MyBusinessObject.created != True)
{
    System.Threading.Thread.Sleep(500);
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:thready
ID: 40582908
Of course!  Thank you  :)
0
 
LVL 1

Author Comment

by:thready
ID: 40582976
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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 23

Expert Comment

by:Michael74
ID: 40582991
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

0
 
LVL 1

Author Comment

by:thready
ID: 40583072
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.
0
 
LVL 23

Expert Comment

by:Michael74
ID: 40583084
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.
0
 
LVL 1

Author Comment

by:thready
ID: 40583093
Thank you again for your help!  Much appreciated.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now