Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispatcher.BeginInvoke , trying to use lambda to get string set from textblock, but getting conversion error

I am trying to call a selected listbox item from a button, not the listbox.selecteditemchanged method in wpf. So when i try

string yadda = listbox.SelectedItem.ToString();

i get an exception:

The calling thread cannot access this object because a different thread owns it.

So, what i was trying to do is the following:

Dispatcher.BeginInvoke(() =>
                    {
                        lbxSelectedItem =  (lbxFileList.SelectedItem as TextBlock).Text;
                    });

That is not working either because i get another exception:

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

like image 707
darthwillard Avatar asked May 31 '11 00:05

darthwillard


1 Answers

Convert the lambda to an Action:

Dispatcher.BeginInvoke((Action)(() => { /*DoStuff*/ }));

Or construct one from the lambda:

Dispatcher.BeginInvoke(new Action(() => { /*DoStuff*/ }));

You probably can write an extension method for the Dispatcher that takes an Action, that way the lambda would be implicitly converted.

like image 104
H.B. Avatar answered Oct 17 '22 02:10

H.B.