Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Delegate and Dispatcher problem

Tags:

c#

i get this error when trying this:

ERROR method name expected.

How should i do to correct the problem

delegate void DelegateFillList(DeliveryDoc[] deliveryDocs);
private void FillListViewAssignment(DeliveryDoc[] docs) {
    if(lvMyAssignments.Dispatcher.CheckAccess()) {
        lvMyAssignments.ItemsSource = docs;
        lvAllOngoingAssignments.ItemsSource = docs;

        if(m_tempDeliveryDocs != null) {
            txtblockHandOverCount.Text = m_tempDeliveryDocs.Length.ToString();
        }

    } else {
        lvMyAssignments.Dispatcher.BeginInvoke(
            new DelegateFillList(FillListViewAssignment(docs)), null);
    }
}
like image 425
Tan Avatar asked Feb 28 '23 04:02

Tan


1 Answers

This is the problem:

new DelegateFillList(FillListViewAssignment(docs)

You can't create a delegate that way. You need to provide a method group which is just the name of the method:

 lvMyAssignments.Dispatcher.BeginInvoke
     (new DelegateFillList(FillListViewAssignment), new object[]{docs});

Alternatively, you could do it in two statements:

DelegateFillList fillList = FillListViewAssignment;
lvMyAssignments.Dispatcher.BeginInvoke(fillList, new object[]{docs});

The reason for the extra "wrapping" array is that you've only got one argument, which is an array - you don't want it to try to interpret that as a bunch of different arguments.

like image 84
Jon Skeet Avatar answered Mar 07 '23 16:03

Jon Skeet