等待ShowDialog返回时同步一段代码

发布于 2024-09-04 02:27:34 字数 653 浏览 6 评论 0原文

我无法弄清楚如何在应用程序等待外部程序的响应时将其锁定在一段代码之外,

我在一段带有 的代码上使用了 Synclock表达式中的 Me 对象。在此 Synclock 中,我调用对话框的重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-09-11 02:27:34

我个人不使用 VB.NET 的同步锁功能,因为我发现它很挑剔。我喜欢创建一个表单范围布尔值:

dim lock as boolean = false

然后我利用这个布尔值作为我的同步锁,如下例所示。

 Sub LockUntilShowDialogOkSelected()
    If Not lock Then
      lock = True
      Dim frmDlgWithTimeout As SaveFileDialog ' dialog box with overridden ShowDialog '

      If frmDlgWithTimeout.ShowDialog = Windows.Forms.DialogResult.OK Then
        lock = False
      End If
    End If
  End Sub

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:

dim lock as boolean = false

I then utilize this boolean as my synchlock, as in the below example.

 Sub LockUntilShowDialogOkSelected()
    If Not lock Then
      lock = True
      Dim frmDlgWithTimeout As SaveFileDialog ' dialog box with overridden ShowDialog '

      If frmDlgWithTimeout.ShowDialog = Windows.Forms.DialogResult.OK Then
        lock = False
      End If
    End If
  End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文