Device is Already Opened

Questions, comments and suggestions concerning VintaSoft Twain .NET SDK.

Moderator: Alex

Post Reply
simp513
Posts: 4
Joined: Thu Oct 04, 2012 11:06 pm

Device is Already Opened

Post by simp513 » Thu Dec 19, 2013 11:40 pm

My scan code:

Code: Select all

private bool Scan()
{
    bool ret = false;
    string lastStatus = "None";
    try
    {
        if (File.Exists(@"C:\Scan.tif"))
            File.Delete(@"C:\Scan.tif");
        _sessionCount = 0;
        _imageCount = 0;

        // open the device manager
        _deviceManager.Open();
        // if no devices are found in the system
        if (_deviceManager.Devices.Count == 0)
        {
            MessageBox.Show("No devices found.");
            return ret;
        }

        // get reference to current device
        _device = _deviceManager.Devices.Current;
        
        // subscribe to the device events
        SubscribeToDeviceEvents();

        // set scanning settings 
        _device.ShowUI = false;
            
        _device.Open();
        //_device.PixelType = PixelType.BW;
        _device.UnitOfMeasure = UnitOfMeasure.Inches;
        _device.DisableAfterAcquire = true;
        _device.TransferMode = TransferMode.Memory;
        _device.AcquiredImages.AutoClean = true;
        // get reference to object that manipulates DuplexEnabled capability 
        DeviceCapability duplexEnabledCap = _deviceManager.Devices.Current.Capabilities.Find(DeviceCapabilityId.DuplexEnabled);
        // if DuplexEnabled capability supported 
        if (duplexEnabledCap != null)
        {
            // set value of DuplexEnabled capability 
            duplexEnabledCap.SetValue(checkBox1.Checked);
        }
        

        string tiffFilename = @"C:\scan.tif";

        // acquire images from device 
        AcquireModalState acquireModalState = AcquireModalState.None;
        AcquiredImage acquiredImage = null;
        do
        {
            acquireModalState = _device.AcquireModal();
            switch (acquireModalState)
            {
                case AcquireModalState.ImageAcquired:
                    lastStatus = "ImageAcquired";
                    acquiredImage = _device.AcquiredImages.Last;
                    // despeckle/deskew/detect border 
                    acquiredImage.Despeckle(8, 25, 30, 400);
                    acquiredImage.Deskew(BorderColor.AutoDetect, 5, 5);
                    acquiredImage.DetectBorder(5); 
                    // add image to multipage TIFF file if image is not blank 
                    if (!acquiredImage.IsBlank(0.02f))
                        acquiredImage.Save(tiffFilename);
                    ret = true;
                    break;
                    
                case AcquireModalState.ScanCompleted:
                    lastStatus = "ScanCompleted";
                    ret = true;
                    //Copy Images
                    //Clear images
                    _device.AcquiredImages.Clear();
                    // close the device 
                    _device.Close();
                    // close the device manager 
                    _deviceManager.Close();
                    break;
                case AcquireModalState.ScanCanceled:                                
                case AcquireModalState.ScanFailed:
                    lastStatus = "ScanFailed";
                    ret = false;
                    //Copy Images
                    //Clear images
                    _device.AcquiredImages.Clear();
                    // close the device 
                    _device.Close();
                    // close the device manager 
                    _deviceManager.Close();
                    break;
            }
        }
        while (acquireModalState != AcquireModalState.None);

        UnsubscribeFromDeviceEvents();

        if (acquiredImage != null)
            acquiredImage.Dispose();
    }
    catch (TwainException ex)
    {
        MessageBox.Show(ex.Message , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //ERROR Comes here
    }
    return ret;
}
How can I stop getting "Device is Already Opened"? It seems to happen randomly, maybe 1 page maybe on the 5th scan. Any help?

Alex
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Re: Device is Already Opened

Post by Alex » Fri Dec 20, 2013 8:58 am

Hello,

Please debug your application, check the device status each time when you start an image acquisition and check why device is opened when you starting the image acquisition.

Best regards, Alexander

simp513
Posts: 4
Joined: Thu Oct 04, 2012 11:06 pm

Re: Device is Already Opened

Post by simp513 » Fri Dec 20, 2013 4:05 pm

ok, where would you suggest I insert that?

Alex
Site Admin
Posts: 1445
Joined: Thu Jul 10, 2008 2:21 pm

Re: Device is Already Opened

Post by Alex » Sat Dec 21, 2013 8:59 am

Hello,

Your Scan() method opens TWAIN device manager, selects and opens the device, acquires image from device, closes the device and TWAIN device manager. You may have the "Device is Already Opened" error only if you called method Scan() twice at the same time. Are you use Scan() method in multi-threading environment? Or maybe you do not disable the "Scan" button when it clicked and user can click the button twice? Please examine your code.

Best regards, Alexander

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests