Stuck at transferring images

Issue #82 resolved
Vincent created an issue

Only on a printer Brother ADS-2000, when i start the scan process, i get stuck on the transferring process. No progress window is shown, and the scanner is not doing anything.

The scanner works well when used with other softwares, and even with the librairy TwainDotNet.

Anyone could have a look at my code to see if i missed something? Is stays stuck in the loop "while (tSession.IsTransferring)", and i receive nothing in the "tSession_DataTransferred" event, and there is no error either...

private void StartScanProcessNTwain()
{
 try
 {
    _imagesNTwain.Clear();

    TwainSession tSession = null;
    NTwain.DataSource tDs = null;
    try
    {
       // new it up and handle events
       LoggingHelper.LogError("new it up and handle events");
       var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly());
       tSession = new TwainSession(appId);
       tSession.SynchronizationContext = SynchronizationContext.Current;
       tSession.TransferReady += tSession_TransferReady;
       tSession.DataTransferred += tSession_DataTransferred;
       tSession.TransferError += tSession_TransferError;

       // finally open it
       LoggingHelper.LogError("finally open it");
       var result = tSession.Open();
       if (result != ReturnCode.Success)
          throw new ScociException(result.ToString());

       // Get first datasource
       LoggingHelper.LogError("Get first datasource");
       tDs = tSession.FirstOrDefault(x => x.Name == Program.Settings.Scanner.Name);
       if (tDs == null)
          throw new ScociException("Impossible de trouver le scanneur: " + Program.Settings.Scanner.Name);

       // Open de datasource
       LoggingHelper.LogError("Open de datasource");
       result = tDs.Open();
       if (result != ReturnCode.Success)
          throw new ScociException(result.ToString());

       LoggingHelper.LogError("Set properties");
       // Duplex
       tDs.Capabilities.CapDuplexEnabled.SetValue(Program.Settings.ScanSettingsNTwain.Duplex ? BoolType.True : BoolType.False);

       // Color type
       tDs.Capabilities.ICapPixelType.SetValue(Program.Settings.ScanSettingsNTwain.ColorSetting);

       // Paper type
       tDs.Capabilities.ICapSupportedSizes.SetValue(Program.Settings.ScanSettingsNTwain.PaperSize);

       // DPI
       foreach (var val in tDs.Capabilities.ICapXResolution.GetValues())
       {
          if ((int)val == Program.Settings.ScanSettingsNTwain.Dpi)
          {
             tDs.Capabilities.ICapXResolution.SetValue(val);
             tDs.Capabilities.ICapYResolution.SetValue(val);
             break;
          }
       }

       tDs.Capabilities.ICapAutomaticBorderDetection.SetValue(BoolType.True);
       tDs.Capabilities.ICapAutomaticRotate.SetValue(BoolType.True);
       tDs.Capabilities.ICapImageFileFormat.SetValue(FileFormat.Bmp);
       if (tDs.Capabilities.ICapCompression.IsSupported && tDs.Capabilities.ICapCompression.CanSet)
          tDs.Capabilities.ICapCompression.SetValue(CompressionType.Jpeg);

       LoggingHelper.LogError("Start Scan");
       result = tDs.Enable(SourceEnableMode.NoUI, false, this.Handle);
       if (result != ReturnCode.Success)
          throw new ScociException(result.ToString());

       // Attendre que le scan soit terminé
       LoggingHelper.LogError("Wait for transfert");
       while (tSession.IsTransferring)
       {
          Application.DoEvents();
          Thread.Sleep(1000);
       }
    }
    catch (Exception ex)
    {
       LoggingHelper.Log(ex);
       ShowMessageBox(ex.Message, My.Resources.Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
       if (tDs != null)
       {
          tDs.Close();
          tDs = null;
       }

       if (tSession != null)
       {
          tSession.Close();
          tSession = null;
       }
    }
    LoggingHelper.LogError("Done! Nb: " + _imagesNTwain.Count);
}

private void tSession_TransferError(object sender, TransferErrorEventArgs e)
{
 LoggingHelper.Log(e.Exception);
}

private void tSession_DataTransferred(object sender, DataTransferredEventArgs e)
{
 _imagesNTwain.Add(e.GetNativeImageStream());
}

private void tSession_TransferReady(object sender, TransferReadyEventArgs e)
{

}

Comments (3)

  1. Eugene Wang repo owner

    Is it really waiting in the loop? Try using

    tSession.State > 4
    

    as the condition instead.

  2. Vincent reporter

    You pointed me to it! The flag IsTranferring is not set to "true" until the State property property is set to 6. So basically, i was never enterring the loop and closing the Datasource right away. Which caused the driver to freeze.

    Thank you, it is fixed now.

  3. Log in to comment