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

Comments

Projects
None yet
3 participants
@gordon771

gordon771 commented Feb 2, 2017

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

This comment has been minimized.

Show comment
Hide comment
@punker76

punker76 Feb 2, 2017

Member

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

Member

punker76 commented Feb 2, 2017

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

@gordon771

This comment has been minimized.

Show comment
Hide comment
@gordon771

gordon771 Feb 3, 2017

Hi,

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

gordon771 commented Feb 3, 2017

Hi,

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

@punker76

This comment has been minimized.

Show comment
Hide comment
@punker76

punker76 Feb 3, 2017

Member

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

Member

punker76 commented Feb 3, 2017

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

@gordon771

This comment has been minimized.

Show comment
Hide comment
@gordon771

gordon771 commented Feb 3, 2017

Here it comes...
MahAppsProblem.zip

@thoemmi thoemmi self-assigned this Feb 3, 2017

@thoemmi thoemmi added the Bug label Feb 3, 2017

@punker76 punker76 added this to the 1.5.0 milestone Feb 3, 2017

@punker76 punker76 closed this in #2840 Feb 5, 2017

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