This is difficult to describe and I hope some serious guru can help me.

My application is erasing an image in a PictureBox due to some control flow or event sequencing problem. I suspect that event triggering requires some Windows OS activity, probably using interrupt logic. I also suspect that the OS activity is not fast enough to prevent certain sequencing problems.

I have an application which draws an image in a PictureBox. While the Draw Subroutine is active, Focus gets set to the PictureBox, which seems to be reasonable.

The PictureBox Lost Focus Event is used to trigger activity which erases the picture.

To prevent the obvious problem, the Draw Subroutine sets a Boolean variable (DrawingInProgress) to True, and the PictureBox Lost Focus Subroutine tests that Boolean variable, intending to avoid erasing the image at the wrong time.

After the Draw Subroutine is finished generating the image, it sets focus to a Command Button, causing the PictureBox Lost Focus Subroutine to activate. After setting Focus to the Command Button, the Draw Subroutine resets DrawingInProgress to False.

I expected the above code sequence to avoid erasure of the image, but it did not.

I wrote some debugging code which stored data in an array to indicate the order of code execution and event triggering. The data indicated that the PictureBox Lost Focus Subroutine activated after the Boolean variable was reset to False.

The following suggests the code being executed.
VB Code:
  1. Private Sub FractalPainter()
  2. [b]. . .[/b]
  3. DrawingInProgress = True
  4.  
  5. ‘Code to generate the image using PSet
  6.  
  7. cmdShuffle.SetFocus
  8. ‘A few lines of code
  9. DrawingInProgress = False
  10. ‘A few lines of code
  11. End Sub
  12. [b]. . .[/b]
  13. Private Sub pixFractal_LostFocus()
  14.  
  15. If DrawingInProgress Then
  16.         Exit Sub
  17.     End If
  18. ‘Code which erases image
  19.  
  20. End Sub
The Lost Focus Subroutine finds the Boolean to be false and erases the image.

Does anybody understand my problem?

Does anybody have a suggestion about what to do?