如何使用Dispatcher.Invoke返回一个值?

时间:2022-08-27 18:46:57

Anyone knows how to return a value from Dispatcher.Invoke in ? I want to return the selected index for a ComboBox.

任何人都知道如何从Dispatcher返回值。在wpf调用吗?我想返回选择的ComboBox索引。

Thanks!

谢谢!

5 个解决方案

#1


27  

int result = -1;

// this is synchronous
myCombo.Invoke(() => 
{
  result = myCombo.SelectedIndex;
});

return result;

This is, of course, kind of clunky. Better design would be to implement INotifyPropertyChanged in your VM, create a SelectedIndex property and bind the SelectedIndex property of your combo box to it. INPC binds are thread-insensitive (3.5 or 4.0+, I don't remember which), so you can read and update these properties from different threads in your VM without worry.

当然,这有点笨拙。更好的设计是在您的VM中实现INotifyPropertyChanged,创建一个SelectedIndex属性并将您的组合框的SelectedIndex属性绑定到它。INPC绑定是不敏感的线程(3.5或4.0+,我不记得是哪个),所以您可以从VM中的不同线程读取和更新这些属性,而不必担心。

#2


24  

There's another way that returns value from Invoke():

还有一种方法可以从调用()返回值:

object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () =>
    {
        return container.IsLoaded;
    })
);

And by the way, chances are that the initial code (which is working with delegate) won't modify oIsLoaded at all; So I'd rather use a Func<> for returning a value from that kind of function.

顺便说一下,很有可能初始代码(与委托一起工作)根本不会修改oIsLoaded;因此,我宁愿使用Func<>来返回这类函数的值。

#3


2  

This is my method to retrieve selected value for a combobox, how can I say delegate to return value?

这是我为combobox检索选定值的方法,我怎么能说委托返回值呢?

    private object getValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        obj = cb.SelectedValue;
                    }
                ));

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

#4


1  

I have solved this. The solution is create a custom delegate that returns the desired type like this:

我已经解决了。解决方案是创建一个自定义委托,该委托返回所需的类型如下:

    private object GetValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj = null;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  (MyDelegate)
                    delegate()
                    {
                        return (obj = cb.SelectedValue);
                    }
                );

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

    public delegate object MyDelegate();

#5


0  

You can't do this directly but you can do this.

你不能直接这么做,但你可以这么做。

Dispatcher.Invoke() actually returns the return value from the delegate you call, so alter your delegate accordingly.

调用()实际上返回您调用的委托的返回值,因此相应地修改您的委托。

Return Value

返回值

Type: System.Object The return value from the delegate being invoked or null if the delegate has no return value.

类型:系统。对象被调用的委托的返回值,如果委托没有返回值,则为null。

Source

#1


27  

int result = -1;

// this is synchronous
myCombo.Invoke(() => 
{
  result = myCombo.SelectedIndex;
});

return result;

This is, of course, kind of clunky. Better design would be to implement INotifyPropertyChanged in your VM, create a SelectedIndex property and bind the SelectedIndex property of your combo box to it. INPC binds are thread-insensitive (3.5 or 4.0+, I don't remember which), so you can read and update these properties from different threads in your VM without worry.

当然,这有点笨拙。更好的设计是在您的VM中实现INotifyPropertyChanged,创建一个SelectedIndex属性并将您的组合框的SelectedIndex属性绑定到它。INPC绑定是不敏感的线程(3.5或4.0+,我不记得是哪个),所以您可以从VM中的不同线程读取和更新这些属性,而不必担心。

#2


24  

There's another way that returns value from Invoke():

还有一种方法可以从调用()返回值:

object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () =>
    {
        return container.IsLoaded;
    })
);

And by the way, chances are that the initial code (which is working with delegate) won't modify oIsLoaded at all; So I'd rather use a Func<> for returning a value from that kind of function.

顺便说一下,很有可能初始代码(与委托一起工作)根本不会修改oIsLoaded;因此,我宁愿使用Func<>来返回这类函数的值。

#3


2  

This is my method to retrieve selected value for a combobox, how can I say delegate to return value?

这是我为combobox检索选定值的方法,我怎么能说委托返回值呢?

    private object getValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        obj = cb.SelectedValue;
                    }
                ));

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

#4


1  

I have solved this. The solution is create a custom delegate that returns the desired type like this:

我已经解决了。解决方案是创建一个自定义委托,该委托返回所需的类型如下:

    private object GetValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj = null;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  (MyDelegate)
                    delegate()
                    {
                        return (obj = cb.SelectedValue);
                    }
                );

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

    public delegate object MyDelegate();

#5


0  

You can't do this directly but you can do this.

你不能直接这么做,但你可以这么做。

Dispatcher.Invoke() actually returns the return value from the delegate you call, so alter your delegate accordingly.

调用()实际上返回您调用的委托的返回值,因此相应地修改您的委托。

Return Value

返回值

Type: System.Object The return value from the delegate being invoked or null if the delegate has no return value.

类型:系统。对象被调用的委托的返回值,如果委托没有返回值,则为null。

Source