Dispatcher.Invoke blocks forever

I am trying to call the UI manager dialog:

class DialogService : IDialogService
{
    private readonly Dispatcher _dispatcher = Application.Current.Dispatcher;

    public bool? Show(IDialogViewModel viewModel)
    {
        if (_dispatcher.CheckAccess())
        {
            var dialogWindow = new DialogWindow();
            return dialogWindow.Show(viewModel);
        }
        else
        {
            Func<IDialogViewModel, bool?> func = Show;
            return (bool?)_dispatcher.Invoke(func, viewModel);
        }
    }
}

However, the call is Invokeblocked forever, and is Shownever called in the user interface thread ...

Using is BeginInvokenot an option: I need the result immediately because I am handling the event from a remote object (using remote .NET)

Any idea?


UPDATE

Here is a more complete description of the problem:

, Windows .NET Remoting. - ( , ). : CredentialsNeeded, . . , .

, , ... , , Invoke , ? , ? WinForms , Application.Run, , WPF...

+3
3

: . :

class DialogService : IDialogService
{
    private readonly Dispatcher _dispatcher = Application.Current.Dispatcher;

    public bool? Show(IDialogViewModel viewModel)
    {
        if (_dispatcher.CheckAccess())
        {
            DoShow(viewModel);
        }
        else
        {
            bool? r = null;
            Thread thread = new Thread(() => r = DoShow(viewModel));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return r;
        }
    }

    private static bool? DoShow(IDialogViewModel viewModel)
    {
        var dialogWindow = new DialogWindow();
        return dialogWindow.Show(viewModel);
    }
}
+1

, ? , , .

? , , .

, : break , .

, , ... , BeginInvoke ( )? ? , , .

+4

- (, ) , Invoke? , . , .

Windows Forms " " , , , - .

, , . Threads . , " ", , .

DispatcherPriority of Send, , , .

+1
source

Source: https://habr.com/ru/post/1751249/


All Articles