Solved

C# async & await, WPF application

Posted on 2015-02-01
7
142 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

861 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

23 Experts available now in Live!

Get 1:1 Help Now