在下面的代码中,如果有未设置的内容,我会显示有关我的应用程序的信息 (...在其中一个类或方法中)正确地弹出一个窗口,其中包含当前消息,告诉您缺少什么。
只有一个问题我想知道这是否以及如何完成,应用程序在仍在调试时被卡住,所以我无法移动窗口或单击它的控件,
您有什么解决办法我可以申请吗?
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/