WinForms Control.BeginInvoke asynchronous callback

Posted by Darran on Stack Overflow See other posts from Stack Overflow or by Darran
Published on 2010-03-01T09:24:03Z Indexed on 2010/03/21 5:31 UTC
Read the original article Hit count: 488

Filed under:
|
|
|

I have a number of Janus grid controls that need to be populated on an application startup.

I'd like to load these grids on different threads to speed startup time and the time it takes to refresh these grids. Each grid is on a seperate tab. Ideally I'd like to use Control.BeginInvoke on each grid and on the grid load completing the tabs will become enabled.

I know with Delegates you can do a Asynchronous callback when using BeginInvoke, so I could enable the tabs in the asynchronous callback, however when using Control.BeginInvoke this is not possible. Is there a way to do asynchronous callbacks using Control.BeginInvoke or possibly a better solution?

So far I have:

public delegate void BindDelegate(IMyGrid grid);

private IAsyncResult InvokeBind(IMyGrid grid)
{
  return ((Control)grid).BeginInvoke(
    new BindDelegate(DoBind), new object[] { grid }
  );
}

private void DoBind(IMyGrid grid)
{
  grid.Bind(); // Expensive operation
}

private void RefreshComplete()
{
  IAsyncResult grid1Asynch = InvokeBind(grid1);
  IAsyncResult grid2Asynch = InvokeBind(grid2);
  IAsyncResult grid3Asynch = InvokeBind(grid2);
  IAsyncResult grid4Asynch = InvokeBind(grid3);
  IAsyncResult grid5Asynch = InvokeBind(grid4);
  IAsyncResult grid6Asynch = InvokeBind(grid5);
}

Now I could spin off a separate thread and keep checking to see if the IAsynchResults have completed and depending on which one completes I could re-enable the Tab control that the grid is contained in. Is there a better way of doing this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about control