Home › Forums › TWAIN Classic › Twain Application, Windows C#, Callbacks & Win Message Loop
- This topic has 1 reply, 2 voices, and was last updated 10 years, 4 months ago by spike.
- AuthorPosts
I am writing a C# Twain application for Windows. Using TWAINDSM.DLL, what is the recommended method for handling events from the DSM and/or DS? The Twain 2.n Spec discusses both:
1 Using Callbacks
2.Using Windows Message Loop eventsI want to use Callbacks only, however there are statements in the Twain 2.n Spec that imply Windows applications MUST use Windows Message Loop events. For example, quoting from the Twain 2.n Spec:
• On Windows – while the application has the Source enabled, the application is forwarding all
events in its event loop to the Source by using the DG_CONTROL / DAT_EVENT /
MSG_PROCESSEVENT operation.Additionally, while examining the Sample application source code, the following code section indicate again that the Windows Message Loop events must be used:
#ifdef TWNDS_OS_WIN
// now we have to wait until we hear something back from the DS.
while(!gpTwainApplicationCMD->m_DSMessage)
{
TW_EVENT twEvent = {0};
// If we are using callbacks, there is nothing to do here except sleep
// and wait for our callback from the DS. If we are not using them,
// then we have to poll the DSM.
// Pumping messages is for Windows only0
MSG Msg;
if(!GetMessage((LPMSG)&Msg, NULL, 0, 0))
{
break;//WM_QUIT
}
twEvent.pEvent = (TW_MEMREF)&Msg;
twEvent.TWMessage = MSG_NULL;
TW_UINT16 twRC = TWRC_NOTDSEVENT;
twRC = _DSM_Entry( gpTwainApplicationCMD->getAppIdentity(),
gpTwainApplicationCMD->getDataSource(),
DG_CONTROL,
DAT_EVENT,
MSG_PROCESSEVENT,
(TW_MEMREF)&twEvent);
if(!gUSE_CALLBACKS && twRC==TWRC_DSEVENT)
{
// check for message from Source
switch (twEvent.TWMessage)
{
case MSG_XFERREADY:
case MSG_CLOSEDSREQ:
case MSG_CLOSEDSOK:
case MSG_NULL:
gpTwainApplicationCMD->m_DSMessage = twEvent.TWMessage;
break;
default:
cerr << "nError - Unknown message in MSG_PROCESSEVENT loopn" << endl;
break;
}
}
if(twRC!=TWRC_DSEVENT)
{
TranslateMessage ((LPMSG)&Msg);
DispatchMessage ((LPMSG)&Msg);
}
}
#endifBoth the Twain 2.n Spec and the Sample Application imply that for Window Applications, when using callbacks, the application still must use the Windows event loop to capture events from the DSM and DS. Can you please confirm how a Windows C# application (running in a background thread too), uses only callbacks to handle events from DSM and DS. Can you please confirm that a Windows C# application does not need any Windows message loop hokey-pokey 😳 when using callbacks to handle events.
Very belatedly, in the hope it is useful to somebody else:
You still have to intercept the Windows message pump and route messages to TWAIN, even if you use TWAIN 2.0 callbacks to handle events from TWAIN.
Guess I should mention that one way to do that is to run your own message pump, but that does get kind of tricky, and is best done on a dedicated thread.
For those working in .NET/Mono, Mark has published a comprehensive TWAIN wrapper called TWAIN for C-Sharp (C#) on SourceForge, with two sample applications:
http://sourceforge.net/projects/twainforcsharp/?source=directory- AuthorPosts