pass delegate as arg to method?

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;
 
J

Jon Skeet [C# MVP]

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");
}
}
 

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