Pause a window like MessageBox.Show()

最后都变了- 提交于 2019-12-10 11:19:47

问题


I have a scripting language implemented in WPF that allows a user to script multiple concurrent actions. These actions are implemented using async/await and all of them run on the main thread.

I'm now introducing a step debugger into this script language. Currently, there is one window associated with the script engine and a different window for the step debugger.

I'm trying to accomplish stopping processes in the engine window just before a scripted action executes while not blocking interactivity on the debugger window. MessageBox.Show() works great for this especially because it pauses animations which may themselves start new actions on completion.

Thread.Sleep() of course doesn't work at all because everyone is on the main thread.

Is there a pausing mechanism in C# similar to MessageBox.Show() but with no UI? I've tried calling Dispatcher.DisableProcessing() which I could have sworn worked to pause the engine window, but now I get the error:

'Dispatcher processing has been suspended, but messages are still being processed'

I think ideally I'd like to do something like this:

//Engine Window
private debugger;
void RunAction(ScriptAction action){
 if(action.BreakPoint){
  var pauserObject = PauseProcessesOnThisWindow();
  debugger.break(action, pauseObject);
}
}

//Debugger Window

void Continue_Click(){
 pauseObject.release();
}

If I have to go with the MessageBox approach, I can see that closing it from the other window can be tricky, not to mention that it's an awkward UI to have some control in the debugger window but still require the user to close the box from the engine window.


回答1:


Personally, I avoid nested message loops. They're considerably dangerous due to reentrancy.

I would recommend using a dedicated thread for your script window (see "Multiple Windows, Multiple Threads" in the WPF Threading Model documentation). Running a separate window in a separate thread is much easier on WPF than it ever was with WinForms.

If you do want a nested message loop, though, you can create one using PushFrame.



来源:https://stackoverflow.com/questions/43921846/pause-a-window-like-messagebox-show

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!