Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# BeginInvoke problem

Tags:

c#

wpf

Why does i have this error and how to fix it. Thanks for help

Error 4 Cannot convert lambda expression to type 'System.Delegate' because it is not a delegate type

    void provider_GetAssignmentsComplete(object sender, QP_Truck_Model.Providers.GetAssignmentsEventArgs e) {
        lvMyAssignments.Dispatcher.BeginInvoke(() =>
        {
            lvMyAssignments.ItemsSource = e.HandOverDocs;
        });
    }
like image 700
Tan Avatar asked Aug 11 '10 15:08

Tan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

Lambda expression are not implicitly convertible to delegates in certain cases. Specifically, if the method expects the type Delegate you have to first explicitly cast the lambda for the compiler to accept it.

What you can do is explicitly cast the lambda, which should allow you to use BeginInvoke:

lvMyAssignments.Dispatcher.BeginInvoke( (Action)(() => 
        { 
            lvMyAssignments.ItemsSource = e.HandOverDocs; 
        })); 

Normally, if you have a method with a strongly-typed delegate signature, like:

public static void BeginInvoke( Action d ) { ... }

The compiler can convert a lambda expression to the appropriate delegate signature needed. But if the method is loosely typed:

public static void BeginInvoke( Delegate d ) { ... }

the compiler will not accept a lambda. You can, however, cast the lambda expression to a specific delegate signature (say Action), and then pass that to the method. The compiler cannot automatically do this for you, because there are many different delegate types that could be a valid match for the signature of the lambda ... and the compiler has no way of knowing which would be the right one.

like image 192
LBushkin Avatar answered Oct 04 '22 02:10

LBushkin