等待ShowDialog返回时同步一段代码
我无法弄清楚如何在应用程序等待外部程序的响应时将其锁定在一段代码之外,
我在一段带有 的代码上使用了
对象。在此 Synclock
表达式中的 MeSynclock
中,我调用对话框的重写 ShowDialog
方法,该方法具有超时参数,但确实从底层 ShowDialog
返回值定时器设置后,函数调用。像这样工作。
SyncLock Me
Dim frmDlgWithTimeout As New frmDlgWithTimeout ' dialog box with overridden ShowDialog '
Dim res As DialogResult = frmDlgWithTimeout.ShowDialog(10 * 1000) ' 10 sec timeout '
End SyncLock
现在,外部程序可能会引发事件,使我的应用程序进入此 Synclock
,但它不会阻止它进入该同步锁,即使 ShowDialog
函数尚未返回值(因此我认为会保持代码部分锁定)。
程序中只有一个用于锁的对象实例。
非常感谢您的帮助。
I'm having trouble working out how to lock my application out of a section of code while it waits for a response from an external program
I've used Synclock
on a section of code with the Me
object in the expression. In this Synclock
I call an overridden ShowDialog
method of a dialog box, which has a timeout parameter, but does return the value from the underlying ShowDialog
function call ,once the timer is setup. Works like this.
SyncLock Me
Dim frmDlgWithTimeout As New frmDlgWithTimeout ' dialog box with overridden ShowDialog '
Dim res As DialogResult = frmDlgWithTimeout.ShowDialog(10 * 1000) ' 10 sec timeout '
End SyncLock
Now, external programs may raise events that bring my application to this Synclock
but it doesn't prevent it from entering it, even though the ShowDialog
function hasn't returned a value (and hence what I thought would keep the section of code locked).
There is only one instance of the object that is used for lock in the program.
Your help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我个人不使用 VB.NET 的同步锁功能,因为我发现它很挑剔。我喜欢创建一个表单范围布尔值:
然后我利用这个布尔值作为我的同步锁,如下例所示。
I personally do not use the synchlock functionality of VB.NET as I have found it to be finicky. I like to create a form scope boolean say:
I then utilize this boolean as my synchlock, as in the below example.