Can anyone explain what I need to do to get past this error?
"Cannot convert anonymous method to type 'System.Windows.Threading.DispatcherPriority' because it is not a delegate type"
private void Test(object sender)
{
base.Dispatcher.BeginInvoke(delegate
{
//some code
}, new object[0]);
}
Thanks
EDIT
I got confused with you passing
new object[0]
as "parameter" toBeginInvoke
and didn't realize that this actually means "no parameters", as everything following the delegate is in theparams
collection...I'm making an example that expects a single integer.
You want to pass parameters, so it's best to use this
This creates an anonymous delegate that takes an integer, converts this to an action that takes an integer and calls the delegate with the parameter
5
.If you're using .NET 3.5 SP1 and upwards, then you can add a reference to System.Windows.Presentation.dll and make sure you have
using System.Windows.Threading;
at the top of the file. It contains extension methods that are easier to use, and allow you to simply write:If you're using .NET 3.5 without SP1 or lower, then you'll have to cast the delegate to a concrete delegate type:
Updated Answer
Cast the delegate to
Action
(or toFunc<something>
if you are returning a value).The first parameter of the
Dispatcher.BeginInvoke
method requires aSystem.Delegate
. This is uncommon. Usually you would specify one of theFunc
orAction
overloads. However, here it is possible to pass delegates with different signatures. Obviously anonymous delegates are not casted toSystem.Delegate
implicitly.UPDATE
I am working with .NET 3.5. In later Framework versions additional overloads of
BeginInvoke
may disturb C#'s overloading mechanism. Try