Multiple DTWAIN Sessions

Top  Previous  Next

You are allowed to make multiple calls to DTWAIN_SysInitialize in different threads.  Each call to DTWAIN_SysInitialize will return a unique handle, Therefore your application can run a DTWAIN session in one thread, and another complete DTWAIN session in another thread.

 

However, a thread that calls DTWAIN_SysInitialize cannot "communicate" with another thread that called DTWAIN_SysInitialize.  For example, if you call DTWAIN_SysInitialize in thread 1, and DTWAIN_SysInitialize in thread 2, you cannot select a TWAIN Source in thread 1 using DTWAIN_SelectSource( ) and use it for a DTWAIN API function in thread 2.

 

For example:

 

//Thread 1 (main thread):

DTWAIN_SysInitialize( );

DTWAIN_SOURCE Source = DTWAIN_SelectSource( );

//....

// Start of thread 2 (assume values from thread 1 have been passed to thread 2)

DTWAIN_CloseSource( Source );  // -1001 error since the Source was selected in thread 1, and we are using it in thread 2

//....

// ....

 

Note that an error is returned, since Source was selected and opened in thread 1, and was attempted to be used in the DTWAIN_CloseSource function in thread 2.  The following scenario is valid:

 

//Thread 1 (main thread):

DTWAIN_UseMultipleThreads(TRUE); // Must be called to ensure proper thread handling

DTWAIN_SysInitialize( );

DTWAIN_SOURCE Source = DTWAIN_SelectSource( );

//....

// Start of thread 2 (assume values from thread 1 have been passed to thread 2)

DTWAIN_SysInitialize( );  // call again for new thread

DTWAIN_SOURCE Source2 = DTWAIN_SelectSource( );

DTWAIN_CloseSource( Source2 );  // OK

//....

// ....

// End of thread 2

 

This scenario calls DTWAIN_SysInitialize( ) for each thread.

 

Before exiting the thread when using multiple sessions of DTWAIN, you must call DTWAIN_SysDestroy.  If not, there will be memory leaks in your application.