Am trying to read the value of a checkbox from a BackgroundWorker in WPF:
This doesn’t work:
bool? isSleepChecked = checkBoxSleep.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate{ return checkBoxSleep.IsChecked;});
Cannot convert anonymous method to delegate type ‘System.Threading.ThreadStart’ because some of the return types in the block are not implicitly convertible to the delegate return type
EDIT – Here is HB’s answer expressed using a delegate instead of lambda which I find slightly more readable
bool? isSleepChecked = (bool?)checkBoxSleep.Dispatcher.Invoke(new Func<bool?>(delegate { return checkBoxSleep.IsChecked; }));
Assign the variable instead, then you don’t need a
return.Alternatively you can use a delegate with return value (e.g. a
Func<T>):(Requires return type cast before .NET 4.5)