Asynchronous problem

B

bernardpace

Hi,

I am using the following code

private void menuItemStart_Click(object sender, System.EventArgs e)
{
DataTable tbRand, tbShow;
int randSize;

....
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.GenerateAllDraws(tbRand, randSize, ref tbShow);
dataGridRandomized.DataSource = tbShow;
....
}

This code works ok, and data is being showed correctly in datagrid.
Now I am trying to convert it to asynchronous code as shown below.

public delegate DataTable GeneratingAllDrawsDelegate(DataTable table,
int tot, ref DataTable show);

private void menuItemStart_Click(object sender, System.EventArgs e)
{
...
GeneratingAllDrawsDelegate dc = new
GeneratingAllDrawsDelegate(DrawEngine.GenerateAllDraws);
AsyncCallback cb = new AsyncCallback(getResultAllDraws);
IAsyncResult ar = dc.BeginInvoke(tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDraws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.DataSource = tbShow;

}

When I execute this code, no data is being shown in datagrid(when
passing tbRand as DataSource same behaviour was given). I also tried
to place the tbShow into a DataSet and write the data to an XML file,
but no file is being created. Also no exception is being rised.

Can someone help me figure my problem out.
Thanks in Advance
 
R

Richard Blewett [DevelopMentor]

The problem is that the async callback is being executed on a worker thread not on the UI thread. When you set the datasource, the datagrid tries to update itself on the wrong thread. You need to ste the datasource on the UI thread by using dataGridRandomized.BeginInvoke and pass it a delegate that will be executed on the UI thread. One common way to do this is to re-use the async callback method like this:

private void getResultAllDraws(IAsyncResult ar)
{
if( dataGridRandomized.InvokeRequired )
{
dataGridRandomized.BeginInvoke(new AsyncCallback(getResultAllDraws), new object[]{ar});
}
else
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.DataSource = tbShow;
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I am using the following code

private void menuItemStart_Click(object sender, System.EventArgs e)
{
DataTable tbRand, tbShow;
int randSize;

...
// schema of tbShow is being loaded from an xsd file
tbRand = DrawEngine.GenerateAllDraws(tbRand, randSize, ref tbShow);
dataGridRandomized.DataSource = tbShow;
...
}

This code works ok, and data is being showed correctly in datagrid.
Now I am trying to convert it to asynchronous code as shown below.

public delegate DataTable GeneratingAllDrawsDelegate(DataTable table,
int tot, ref DataTable show);

private void menuItemStart_Click(object sender, System.EventArgs e)
{
...
GeneratingAllDrawsDelegate dc = new
GeneratingAllDrawsDelegate(DrawEngine.GenerateAllDraws);
AsyncCallback cb = new AsyncCallback(getResultAllDraws);
IAsyncResult ar = dc.BeginInvoke(tbRand, randSize, ref tbShow, cb,
null);
...
}

private void getResultAllDraws(IAsyncResult ar)
{
DataTable tbRand, tbShow;
GeneratingAllDrawsDelegate del;

// schema of tbShow is being loaded from an xsd file

del = (GeneratingAllDrawsDelegate) ((AsyncResult)ar).AsyncDelegate;
tbRand = del.EndInvoke(ref tbShow, ar);
dataGridRandomized.DataSource = tbShow;

}

When I execute this code, no data is being shown in datagrid(when
passing tbRand as DataSource same behaviour was given). I also tried
to place the tbShow into a DataSet and write the data to an XML file,
but no file is being created. Also no exception is being rised.

Can someone help me figure my problem out.
Thanks in Advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top