TWAIN Application Loop Example

Top  Previous  Next

Without using DTWAIN, the application's message loop must be adjusted to support a TWAIN-enabled application.  The message loop for a TWAIN-enabled application would have to be adjusted similar to the example below (this example uses pseudo-code):

 

MSG msg;

 

while (GetMessage(&msg, NULL, 0, 0))

         if ( A Source is Enabled )

                         Check if msg is a TWAIN message;

                         if msg is a TWAIN Message  then begin

                                         Do some TWAIN code depending

                                         on what type of TWAIN message;

                                         This will be if-then or switch

                                         statements (a source of errors!);

                                         DO NOT pass this message to

                                         your application;

                                   end

                         

                         else begin  

                                         // This is a  Windows message

                                         TranslateMessage(&msg);

                                         DispatchMessage(&msg);

                         end

                           

             else   begin

                                  This is a Windows

                         message because no Source

                         is enabled

                         TranslateMessage(&msg);

                         DispatchMessage(&msg);

                       end                  

 

Note that the difference between the traditional event loop and the TWAIN compliant version is a check to see if there are any Sources in 'acquire' mode.  If there are, the application must check if the message is a TWAIN message and act accordingly if it is (the processing of the TWAIN messages is a source of many problems when programming a TWAIN application). If it is not a TWAIN message, the traditional Translate / DispatchMessage is done.  If there are no Sources opened, the traditional Translate / Dispatch is done.

 

The pseudo-code example above is as simple as possible for describing the traditional, TWAIN-enabled application layout.  There are also many variations of the 'background' processing loops that use a combination of PeekMessage and GetMessage that must do similar processing to the code above.  Note the differences between the above implementation and the simplified DTWAIN Application Loop that the application will use instead of code that looks like the above example.

 

Even though the TWAIN compliant version above may be difficult to follow, the mere act of adjusting the application message loop is no problem for C and C++ programmers.  Every C or C++ Windows program has access to the application event loop, whether directly using the raw Windows API, or indirectly through one of the myriad of class libraries such as the Microsoft Foundation Classes (MFC).