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

标签 c# wpf multithreading debugging

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

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

您有什么解决办法我可以申请吗?

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);
}

最佳答案

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

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

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

       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();
      }

关于c# - 在 Visual Studio 中调试时激活自定义新窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35273107/

相关文章:

c# - 如何在不被迫以 Windows 窗体结束的情况下制作动画?

c# - A-star (A*) 的曼哈顿启发式函数

c# - 使用多个表单,在 ASP.NET Web 表单页面上提交按钮

c# - 如何根据特定条件对DataGrid 中的数据进行分组?

c# - WPF Datagrid Column TextBox 允许可为空的数字输入 C#

c++ - 使用线程时 Windows WriteFile 问题

java - 为线程配置 log4j2

c# - 用于缩短代码的动态图像名称

c# - 数据是通过 AJAX 传递给操作的对象数组

WPF - 选项卡中的 xaml 滚动条