soukoku / ntwain

A TWAIN lib for dotnet.
MIT License
117 stars 47 forks source link

Enable Data Source than wait for the scanner finish scanning and return the scan result #20

Open lnvu1983 opened 3 years ago

lnvu1983 commented 3 years ago

Currently, when enable a data source, it will just return the return code and go to the next step, not waiting for the scanner to finish scanning. I want to wait for the scanner to finish scanning (finish all data transfer event and data source is disabled), and then return the result of the scan.

Thank you very much!

jeleleven commented 1 year ago

I went with a basic while loop to accomplish this.

I subscribe to the TwainSession_DataTransferred event and add each TWImageInfo object to an ObservableCollection and then set a boolean value to true during the TwainSession_SourceDisabled event to break out of the loop.

        public void StartScan()
        {
            if (ScanDataSource != null)
            {
                ScanDataSource.Open();
                ScanDataSource.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);

                while (!isComplete) { }

                CloseSession();
            }
        }
        private void TwainSession_DataTransferred(object sender, DataTransferredEventArgs e)
        {
            ImageData.Add(e.ImageInfo);
        }
        private void TwainSession_SourceDisabled(object sender, EventArgs e)
        {
            isComplete = true;
        }

This may block the UI thread on larger scans, but that doesn't apply in my case.