Skip to content

Running Window in another Thread results in a crash #4072

@janwiesemann

Description

@janwiesemann

After updating to 2.4.4 form 2.4.3 following code stopped working:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        ShutdownMode = ShutdownMode.OnExplicitShutdown;
        StartupUri = null;
        
        base.OnStartup(e);

        //this works
        OpenWindow(false);

        //this does not work
        Thread t = new Thread(() =>
        {
            System.Windows.Threading.Dispatcher disp = System.Windows.Threading.Dispatcher.CurrentDispatcher; //Create new Dispatcher

            try
            {
                OpenWindow(true);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debugger.Break();
            }
            finally
            {
                disp.InvokeShutdown();
            }
        });
        t.SetApartmentState(ApartmentState.STA);
        t.IsBackground = true;
        t.Start();
    }

    private void OpenWindow(bool dialog)
    {
        MetroWindow wind = new MetroWindow();
        wind.Width = 100;
        wind.Height = 100;

        if (dialog)
            wind.ShowDialog();
        else
            wind.Show();
    }
}

Running this results in a InvalidOperationException at

   bei System.Windows.Threading.Dispatcher.VerifyAccess()
   bei System.Windows.Application.add_SessionEnding(SessionEndingCancelEventHandler value)
   bei MahApps.Metro.Behaviors.WindowsSettingBehavior.<AssociatedObject_SourceInitialized>b__2_0() in /_/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs:Zeile 55.
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   bei System.Windows.Threading.DispatcherOperation.InvokeImpl()
   bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   bei MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
   bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bei MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   bei System.Windows.Threading.DispatcherOperation.Invoke()
   bei System.Windows.Threading.Dispatcher.ProcessQueue()
   bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   bei System.Windows.Window.ShowHelper(Object booleanBox)
   bei System.Windows.Window.Show()
   bei System.Windows.Window.ShowDialog()
   bei MahError.App.OpenWindow(Boolean dialog) in C:\Users\jan.wiesemann\Desktop\MahError\MahError\App.xaml.cs:Zeile 50.
   bei MahError.App.<OnStartup>b__0_0() in C:\Users\jan.wiesemann\Desktop\MahError\MahError\App.xaml.cs:Zeile 27.

Due to some complex layouts I'm forced to run it in a different Thread. Otherwise all Windows are blocked white WPF initialises the other Window.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions