Asynchronous problem

B

bernardpace

Hi,

I am using the following code in C#,

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(tb­Rand, 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(Dat­aTable table,

int tot, ref DataTable show);

private void menuItemStart_Click(object sender, System.EventArgs e)
{
...
GeneratingAllDrawsDelegate dc = new
GeneratingAllDrawsDelegate(Dra­wEngine.GenerateAllDraws);
AsyncCallback cb = new AsyncCallback(getResultAllDraw­s);
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).AsyncDelegat­e;

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
 
B

bernardpace

Hi, Thanks for your help, but problem was solved in the following way:

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

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

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

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