views:

45

answers:

1

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.

+1  A: 

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
Michael Eakins
I do agree with this method because SyncLock is used on the same Process with multiple threads. Not in different processes.
Jimmie Clark