pass delegate as arg to method?

  • Thread starter Thread starter VM
  • Start date Start date
V

VM

How can I pass a delegate to another method? In my code (win app), i update
my datagrid with the datatable after the method loadAuditFileToTable is
finished executing. Instead, I'd like to be able to to update the grid
continuously (while the table's filling) and someone suggested I pass a
callback to loadAuditFileToTable and run it every X records. How can I do
this? This is a summary of my code:

loadAuditFileToTableDelegate LoadAuditFileToTable = new
loadAuditFileToTableDelegate(loadAuditFileToTable);
LoadAuditFileToTable.BeginInvoke (new AsyncCallback(yourCallback), null);

delegate void GUIUPdate();
void yourCallback(IAsyncResult iar)
{
if (iar.IsCompleted)
{
dataGrid_auditAddress.Invoke (new GUIUPdate(guiUpdateMethod),null);
}
}
private void guiUpdateMethod()
{
this.dataGrid_auditAddress.DataSource = _table_auditAddress;
}
private void loadAuditFileToTable()
{
ZMMatch.Audit zmAudit = new ZMMatch.Audit(NONE, NONE);
_table_auditAddress = zmAudit.OpenAuditAZMFileToView(_SFileName);
//takes some time to complete
}
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 add
500,000 rows */
sAuditRecord = sr.ReadLine();
}
return myTable;
 
VM said:
How can I pass a delegate to another method?

Just declare it as one of the parameters. For instance:

using System;

delegate void DoSomething();

class Test
{

static void Main()
{
Foo (new DoSomething(SayHi));
}

static void Foo(DoSomething wibble)
{
wibble();
}

static void SayHi()
{
Console.WriteLine("Hi");
}
}
 
Back
Top