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

Stuck when launching MetroWindow in a Separate Thread #3944

Closed
fengxingdy opened this issue Oct 22, 2020 · 7 comments
Closed

Stuck when launching MetroWindow in a Separate Thread #3944

fengxingdy opened this issue Oct 22, 2020 · 7 comments
Assignees
Labels
Milestone

Comments

@fengxingdy
Copy link

Describe the bug

This is the code with problem.
SplashThread = new Thread(new ParameterizedThreadStart((state) =>
{
var wnd = new MetroWindow();
wnd.Show();
Dispatcher.Run();
}));
SplashThread.SetApartmentState(ApartmentState.STA);
SplashThread.IsBackground = true;
SplashThread.Start();

Steps to reproduce

Expected behavior

Show a window before main window shown.

Actual behavior

Program stuck in line wnd.Show().if I modify the code like this, use Window instead of MetroWindow.

SplashThread = new Thread(new ParameterizedThreadStart((state) =>
{
var wnd = new Window();
wnd.Show();
Dispatcher.Run();
}));

The window show normally. Is there anything I do wrong?

Environment

MahApps.Metro version: v2.2.0
Windows build number: Win10 1909 [Version 10.0.18363.836]
Visual Studio: 2019 16.7.3
Target Framework: v4.7.2

Screenshots

@fengxingdy fengxingdy added the Bug label Oct 22, 2020
@punker76
Copy link
Member

@fengxingdy Can you create a simple sample with your issue? Thx And keep in mind, if you create windows in different threads that you need to add all necessary resources to this windows.

@fengxingdy
Copy link
Author

@punker76 Thx. I'll try to create a simple sample to check my issue. By the way, this issue was occured when I update MahApps.Metro from v1.6.5 to v2.2.0, the code works well with v1.6.5 does not work with v2.2.0.

@fengxingdy
Copy link
Author

WinodwLauncher-v2.x.zip
WinodwLauncher-v1.x.zip
WinodwLauncher-v1.x.zip and WinodwLauncher-v2.x.zip is code with MahApps.Metro v1.6.5 and v2.3.0. In WinodwLauncher-v2.x, program works well when launching a winodow subclass from System.Windows.Window, but stuch in line where launch window subclass from MahApps.Metro.Controls.MetroWindow. WinodwLauncher-v1.x works well all the time.

@batzen batzen self-assigned this Oct 23, 2020
@batzen
Copy link
Collaborator

batzen commented Oct 23, 2020

That's a bug in MahApps.Metro.
Fixing the bug here revealed another bug in ControlzEx.
Will fix both.

batzen added a commit to batzen/MahApps.Metro that referenced this issue Oct 23, 2020
batzen added a commit to ControlzEx/ControlzEx that referenced this issue Oct 23, 2020
@punker76 punker76 added this to the 2.3.1 milestone Oct 23, 2020
punker76 added a commit that referenced this issue Oct 23, 2020
(GH-3944) Use BeginInvoke to prevent deadlocks in multithreaded cases
@punker76
Copy link
Member

@batzen You mean this?

   at ControlzEx.Standard.HRESULT.ThrowIfFailed(String message) in C:\projects\controlzex\src\ControlzEx\Microsoft.Windows.Shell\Standard\ErrorCodes.cs:line 622
   at ControlzEx.Standard.HRESULT.ThrowLastError() in C:\projects\controlzex\src\ControlzEx\Microsoft.Windows.Shell\Standard\ErrorCodes.cs:line 631
   at ControlzEx.Behaviors.GlowWindowBehavior.UpdateZOrderOfOwner(IntPtr hwndOwner) in C:\projects\controlzex\src\ControlzEx\Behaviors\GlowWindowBehavior.cs:line 389
   at ControlzEx.Behaviors.GlowWindowBehavior.UpdateZOrderOfThisAndOwner() in C:\projects\controlzex\src\ControlzEx\Behaviors\GlowWindowBehavior.cs:line 357
   at ControlzEx.Behaviors.GlowWindowBehavior.AssociatedObjectWindowProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) in C:\projects\controlzex\src\ControlzEx\Behaviors\GlowWindowBehavior.cs:line 312
   at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

@batzen
Copy link
Collaborator

batzen commented Oct 23, 2020

Exactly. Fixed that already. We just have to release a new version of ControlzEx.
I also made the PInvoke-Wrappers we have there appear in the StackTrace again.

@punker76
Copy link
Member

I will close this here for now, the final fix will be available with the next release of ControlzEx.

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

No branches or pull requests

3 participants