Link to home
Start Free TrialLog in
Avatar of EdSoftware
EdSoftwareFlag for United States of America

asked on

MultiThreaded winform handling events

I have an object that interfaces to a TWAIN device.  The object raises events after each scan.  The GUI subscribes to these events to get the image that was scanned, error events, and some other information about the scan.  I execute the functionality in single threaded winform (.net 3.5) application.

Because the scanning can take 10 seconds, and it hogs the thread on the UI so it becomes unresponsive, i would like to run the scan on a background thread.  

Can someone show me how to do this in a way that the events that the Twain object raises are handled safely?

Currently:
1. Create instance of TwScan
2. Hook events to listen to.
3. Invoke StartScan method on the instance
as scanning proceeds, my winform handles the events.

How to modify to operate the TwScan on the background thread, yet have the events raised on the UI thread?
Avatar of bele04
bele04

Why don't you just put the scanning on the background thread then hook the events to the GUI so that the GUI will just be waiting for the events on the background thread to be raised.  While the GUI handles the events you can pause the background thread or implement a queue for the scanned objects so that your GUI will not have a problem handling multiple scans when it isn't done with the event handling of the current scanned item yet.

Avatar of EdSoftware

ASKER

The GUI can not be invoked from a thread other than the main thread.  I am looking for example on plumbing an instance that is running on the background that raises events on the main thread.
SOLUTION
Avatar of bele04
bele04

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial