在 Visual Studio 中调试时激活自定义新窗口

本文关键字:新窗口 窗口 自定义 激活 Studio Visual 调试 | 更新日期: 2023-09-27 18:33:38

在下面的代码中,我显示有关我的应用程序的信息,如果有未设置的内容(...在其中一个类或方法中)正确地弹出一个窗口,其中包含当前消息,告诉缺少什么。

只有一个问题,我想知道是否以及如何做到这一点,应用程序在仍在调试时被冻结,所以我无法移动窗口或单击它的控件,

您认为我可以应用任何解决方法吗

void SomeMainThreadMethod()
{
    new System.Threading.Thread(() => ProcessSomeLongRunningTask()).Start();
}
//then from another helper class
void ProcessSomeLongRunningTask()
{
    Application.Current.Dispatcher.Invoke(new Action(() =>CustomW.MsgBoxShow(" Title ", "Msg")), System.Windows.Threading.DispatcherPriority.Normal);
}

在 Visual Studio 中调试时激活自定义新窗口

这里的问题是您正在主调度程序线程上处理消息框,并且您知道它默认为对话框并从主应用程序窗口中窃取焦点。

因此,您可以尝试在创建的新线程中执行消息框,也可以创建自己的自定义用户控件,其功能与消息框相同,但它不会继承该工具行为。

您仍然可以像创建的那样从辅助线程运行它,但请记住将与主调度程序创建的对象交互的任何内容包装为委托操作

       public void LongProcess()
       {
            Thread t = new Thread(new ThreadStart(
            delegate
               {
                //Complex code goes here
               this.Dispatcher.Invoke((Action)(() =>
               {
                  //Any requests for controls or variables that you need
                  //from the main application running on the main dispatcher
                  //goes here
               }));

                //Finally once you've got the information to return to
                //your user call a message box here and populate the 
                //message accordingly.
                MessageBox.Show("", "");
                //If a message box fails or isn't good enough
                //create your own user control and call it here like:
                usrConMessage msg = new usrConMessage();
                msg.strTitle = "Whatever";
                msg.strContent = "Whatever";
                msg.show(); //Not a dialog so doesn't steal focus
                //That is normally how I would go about providing a
                //unique and polished user experience.
               }
             ));
            t.Start();
      }