private void btnRefreshLocalDB_Click(object sender, EventArgs e) { // Invoke LoadItemFromRemoteDB asynchronously. Pass an AsyncCallback // delegate instance referencing the CallbackHandler method that // will be called automatically when the asynchronous method // completes. Pass a reference to the AsyncExampleDelegate delegate // instance as asynchronous state; otherwise, the callback method // has no access to the delegate instance in order to call // EndInvoke. AsyncLoadItemFromRemoteDB loadItemMethod = LoadItemFromRemoteDB; IAsyncResult asyncResult = loadItemMethod.BeginInvoke(LoadItemFromRemoteDBCallback, loadItemMethod); }
// A method to handle asynchronous completion using callbacks. public void LoadItemFromRemoteDBCallback(IAsyncResult result) { // Extract the reference to the AsyncExampleDelegate instance // from the IAsyncResult instance. This allows you to obtain the // completion data. AsyncLoadItemFromRemoteDB loadItemMethod = (AsyncLoadItemFromRemoteDB)result.AsyncState; // Obtain the completion data for the asynchronous method. DateTime completion = DateTime.MinValue; try { ApplicationParameter.ItemList = loadItemMethod.EndInvoke(result); Items = ApplicationParameter.ItemList.AsEnumerable(); CacheToLocalDB(); RefreshBindingSource(); MessageBox.Show("Local Database is updated!"); } catch { // Catch and handle those exceptions you would if calling // LoadItemFromRemoteDB directly. } }