WPF:在单独的UI线程中启动时,Dialog消失

时间:2015-08-03 12:54:48

标签: c# wpf windows multithreading

我一直在研究一个场景,我们必须在STA Apartment状态的单独线程中启动一个对话框。正在建立儿童父母关系(这是成功的)。

private void ShowDialog(IntPtr parentWindow) {

        Thread staThread = new Thread (() =>
        {
            ChildWindow window = new ChildWindow ();
            window.Closed += (s, e2) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown (DispatcherPriority.Background);

            WindowInteropHelper interOPHelper = new WindowInteropHelper (window);
            //parentWindow is handle of the parent
            interOPHelper.Owner = parentWindow;
            window.ShowDialog ();
            GC.Collect ();
        });
        staThread.Name = "STAThread";
        staThread.SetApartmentState (ApartmentState.STA);
        staThread.Start ();
        staThread.Join ();
        }
}

当对话框启动时,它出现在父窗口的顶部,一切正常,两个对话框(父对象和子对象)都在任务栏中。但是当按下“Windows + D”(快捷键)或“Windows + M”时,子对话框将从任务栏消失。单击任务栏中的父对话框不会还原对话框。即使按Alt + Tab键还原父对话框也不起作用。最终必须从TaskManager中杀死应用程序。

// call this method in separate thread , not in EDT
private void listDirectory(String dir) {
        File file = new File(dir);
        File[] files = file.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory() == true) {
                    System.out.println("Dossier: " + files[i].getAbsolutePath());
                    FirstWindow.lblRepertoire.setText(files[i].getAbsolutePath()); // call in invoke latter
                    this.dircount++;
                } else {
                    System.out.println("  Fichier: " + files[i].getName());
                    FirstWindow.lblFile.setText(files[i].getAbsolutePath());  // call in invoke latter
                   this.filecount++;
                }
                if (files[i].isDirectory() == true && this.recursivePath == true) {
                    this.listDirectory(files[i].getAbsolutePath());
                }
            }
        }
    }    

注意:如果我没有设置窗口对象的所有者(但这不被认为是当然的行为),则不会出现此行为。

0 个答案:

没有答案