Monday, 21 December 2009

Single Line saves the day !

In my recent project, I was assigned to solve the bug reported from customer. Okay let me brief you about the application, its PC application for Ultrasound scanning exclusively used to scan Bladder, when technician scans the bladder transducer captures the 24 scanned images. Mean time we have to show the progress of image capture like “Image 1 of 24” so on..

To show the image capture status, we used the ReadComplete event supplied with transducer driver, but the problem was Readcomplete event used to trigger every 5ms, Each time the application handles this event, it processes all the code associated with that event. All other events wait in the queue.

The with event is while our code handles the it, our application does not respond. This caused the huge problem on low speed processor that it did not update Image capture progress.

For solving this issue we almost tried many ways for almost two days, finally we got solution in just a single line! Yes, its just single line solution! The savior is

System.Windows.Forms.Application.DoEvents()

If we call DoEvents in your code, application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.

Happy coding…