I'm trying to invoke a dialog on the UI dispatcher :
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 to Invoke
blocks forever, and Show
is never called on the UI thread...
Using BeginInvoke
is not an option : I need the result immediately, because I'm handling an event from a remote object (using .NET remoting)
Any idea ?
UPDATE
Here is a more complete description of the problem :
I have an client application that communicates with a Windows service using .NET Remoting. At some point, the client makes a call to the service to perform an operation (this call is triggered by a user action, a click on a button in that case). The service might need credentials to perform the operation: in that case, it raises a CredentialsNeeded
event, handled by the client. The client then shows a dialog to prompt the user for credentials, and sets the appropriate properties in the event's arguments. When the event handler returns, the service uses the credentials to complete the operation, and returns control to the client.
So, when I receive the event, the UI thread is waiting for an operation to complete on the service side... I assume it's the reason why the Invoke
call is not processed, but how can I work around it ? Can I create another UI thread to show the dialog ? In WinForms, I know I could start another message pump with Application.Run
, but I don't know how to do the same in WPF...