Delegation and refreshing UI control

V

VM

How can I display the contents of a datatable in a windows datagrid while
the table is being filled? Here's my summarized code:

delegate void loadAuditFileToTableDelegate();
private void btn_run_Click(object sender, System.EventArgs e)
{
/* In Window form */
loadAuditFileToTableDelegate LoadAuditFileToTable = new
loadAuditFileToTableDelegate(loadAuditFileToTable);
LoadAuditFileToTable.BeginInvoke (null, null);
}
private void loadAuditFileToTable()
{
/* Also in Window form */
ZMMatch.Audit zmAudit = new ZMMatch.Audit();
table_auditAddress = zmAudit.OpenAuditAZMFileToView(_SFileName); //
process that takes 2-3 mins. to run
// datagrid.DataSource = table_auditAddress; //this line doesn't seem to
work during a delegate call
}

public DataTable OpenAuditAZMFileToView(string sFileName)
{
/* ZMMatch class
// open file, create table/columns,etc...
sAuditRecord = sr.ReadLine();
while (sAuditRecord != null)
{
/* Parse/process line, fill row, and add row to table. Loop will run
about 500,000 times by adding 500,000 rows */
sAuditRecord = sr.ReadLine();
}
return myTable;
}
 
K

Kevin Aubuchon

The data needs to be marshalled back to control by *another* callback and
the Control.Invoke method.

LoadAuditFileToTable.BeginInvoke (new AsyncCallback(yourCallback) , null,
null); //use a callback function

void yourCallback(IAsyncResult iar)
{
if (iar.IsCompleted)
{
dataGrid1.Invoke (new GUIUPdate(guiUpdateMethod),null);

}
}

void guiUpdateMethod()
{

dataGrid.DataSource = theData;

}

This code will correctly load the datagrid when LoadAuditFileToTable is
finished. If you want continuous updating,
you should pass a callback to LoadAuditFileToTable() that will be called
every X records, that will then
invoke the datagrid update.

good luck,
kevin aubuchon
 
V

VM

Thanks for the post. It worked.

You also mentioned that if I pass a callback to LoadAuditFileToTable() every
X records, it could be updated continuously. Where would I put that
callback? Also, the BeginInvoke from the code you wrote takes 3 parms while
mine only takes two parms (LoadAuditFileToTable.BeginInvoke (new
AsyncCallback(yourCallback) , null). Wast that a typo or are they different
methods?


LoadAuditFileToTable.BeginInvoke (new AsyncCallback(yourCallback) , null,
null);
void yourCallback(IAsyncResult iar)
{
if (iar.IsCompleted)
{
dataGrid1.Invoke (new GUIUPdate(guiUpdateMethod),null);
}
}
void guiUpdateMethod()
{
dataGrid.DataSource = theData;
}
 

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