Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# async & await, WPF application

Posted on 2015-02-01
7
Medium Priority
?
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:Michael Fowler
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:Michael Fowler
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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