Monday, June 28, 2010

Simplest thread-safe way to set BindingSource's DataSource

// Fire and forget threading. Even the WCF took a long time, 
// your UI won't be locked(the dreaded white window with Not Responding on title bar)
ThreadPool.QueueUserWorkItem(o => 
{                
    var tx = new TheServiceReference.ServiceClient();
    var sh = tx.Sales_OpenByOrNum("aaa");
    // using the this.Invoke will make the BindingSource run on UI thread
    this.Invoke((MethodInvoker)delegate
    {
        bdsHeader.DataSource = sh; 
    });
    MessageBox.Show("Record opened.");
});


Related to Task-based Async Pattern

No comments:

Post a Comment