简体   繁体   English

如何干净地暂停线程,直到另一个线程完成或更改值

[英]How do I cleanly pause a thread until another thread completes or a value is changed

Using C#: I have a class "MsgBox", a special Message Box formatted to my application. 使用C#:我有一个类“MsgBox”,一个特殊的消息框格式化为我的应用程序。 It runs on an instance, the point was so that when I called: 'new MsgBox("Message Text");' 它运行在一个实例上,关键是当我打电话时:'new MsgBox(“Message Text”);' it would pause the calling thread until the form gets a response/closes or whatever. 它会暂停调用线程,直到表单获得响应/关闭或其他什么。 I tried running the Form on another thread and using Thread.Suspend()/Resume() and it freezes the whole computer for a two or three seconds, then works fine. 我尝试在另一个线程上运行Form并使用Thread.Suspend()/ Resume()并将整个计算机冻结两到三秒,然后工作正常。 Same with Thread.Join(). 与Thread.Join()相同。 Is there a more efficient way to hold the thread? 是否有更有效的方式来保持线程?

You can use Form.ShowDialog method to display a modal dialog box in your application. 您可以使用Form.ShowDialog方法在应用程序中显示模式对话框。 When this method is called, the code following it is not executed until after the dialog box is closed. 调用此方法时,直到关闭对话框后才会执行其后面的代码。

The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. 通过将对话框分配给窗体上Button的DialogResult属性或通过在代码中设置窗体的DialogResult属性,可以为对话框分配DialogResult枚举的值之一。 This value is then returned by this method. 然后,此方法返回此值。 You can use this return value to determine how to process the actions that occurred in the dialog box. 您可以使用此返回值来确定如何处理对话框中发生的操作。 For example, if the dialog box was closed and returned the DialogResult.Cancel value through this method, you could prevent code following the call to ShowDialog from executing. 例如,如果关闭对话框并通过此方法返回DialogResult.Cancel值,则可以阻止执行ShowDialog调用后的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM