Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShowMessageAnsync() causes exception #2835

Closed
gordon771 opened this issue Feb 2, 2017 · 4 comments · Fixed by #2840
Closed

ShowMessageAnsync() causes exception #2835

gordon771 opened this issue Feb 2, 2017 · 4 comments · Fixed by #2840
Assignees
Labels
Milestone

Comments

@gordon771
Copy link

What steps will reproduce this issue?

Following exception is thrown in ShowMessageAsync() after update from MahApps.Metro V1.3.0 to V1.4.2:

System.InvalidOperationException wurde nicht von Benutzercode behandelt.
  HResult=-2146233079
  Message=The calling thread cannot access this object because a different thread owns it.
  Source=WindowsBase
  StackTrace:
       at System.Windows.Threading.Dispatcher.VerifyAccess()
       at System.Windows.Application.get_MainWindow()
       at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.DetectTheme(DependencyObject dialog) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\BaseMetroDialog.cs:line 229
       at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.HandleTheme() in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\BaseMetroDialog.cs:line 155
       at MahApps.Metro.Controls.Dialogs.BaseMetroDialog.Initialize(MetroWindow owningWindow, MetroDialogSettings settings) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\BaseMetroDialog.cs:line 135
       at MahApps.Metro.Controls.Dialogs.BaseMetroDialog..ctor(MetroWindow owningWindow, MetroDialogSettings settings) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\BaseMetroDialog.cs:line 93
       at MahApps.Metro.Controls.Dialogs.MessageDialog..ctor(MetroWindow parentWindow, MetroDialogSettings settings) in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\MessageDialog.cs:line 27
       at MahApps.Metro.Controls.Dialogs.DialogManager.<>c__DisplayClass2_0.<ShowMessageAsync>b__1() in d:\projects\git\MahApps.Metro\src\MahApps.Metro\MahApps.Metro.Shared\Controls\Dialogs\DialogManager.cs:line 164
       at System.Windows.Threading.DispatcherOperation`1.InvokeDelegateCore()
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()

The ShowMessageAsync() has to be shown in a sub MetroWindow. The sub window is running within his own thread and Dispatcher context:

Thread thread = new Thread(() =>
	{                                       
		SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher));  // create and install a context

		WindowBase dialog    = (WindowBase)dllAssembly.CreateInstance(dialogClass);					
		dialog.Show();					      

		Dispatcher.Run();                   
	});

thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = false;

thread.Start(); 

So the access to the applications main window (System.Windows.Application.get_MainWindow()) is problematic. My code is working with V1.3.0 of MahApps flawless.

Expected outcome

Same behaviour like in previous major version 1.3.0 => no exception

--

Environment

  • MahApps.Metro v1.4.2
  • Windows 7
  • Visual Studio 2013
  • .NET Framework 4.6.2

--

Thank you!!!

Thank you very much for your amazing and absolutely impressiv GUI framework, which is gives the right touch to the standard WPF GUI._

@punker76
Copy link
Member

punker76 commented Feb 2, 2017

@gordon771 Can you provide your app or create a short sample? Would be helpful. thx

@gordon771
Copy link
Author

Hi,

I created a small VS project where you can reproduce the exception. How can I provide it to you?

@punker76
Copy link
Member

punker76 commented Feb 3, 2017

@gordon771 put it on GutHub or drop a zip here as comment

@gordon771
Copy link
Author

Here it comes...
MahAppsProblem.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

3 participants