开发者

Synclock a section of code while waiting for ShowDialog to return

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 appreci开发者_运维问答ated.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜