Added xfer error event instead of throwing exceptions during xfers.

This commit is contained in:
soukoku
2014-04-12 11:07:13 -04:00
parent 2a72794eb2
commit ec3bb72424
3 changed files with 61 additions and 4 deletions

View File

@@ -2189,12 +2189,12 @@ namespace NTwain.Data
/// <summary> /// <summary>
/// Condition Code describing the status. /// Condition Code describing the status.
/// </summary> /// </summary>
public ConditionCode ConditionCode { get { return (ConditionCode)_conditionCode; } set { _conditionCode = (ushort)value; } } public ConditionCode ConditionCode { get { return (ConditionCode)_conditionCode; } internal set { _conditionCode = (ushort)value; } }
/// <summary> /// <summary>
/// Valid for TWAIN 2.1 and later. This field contains additional /// Valid for TWAIN 2.1 and later. This field contains additional
/// scanner-specific data. If there is no data, then this value must be zero. /// scanner-specific data. If there is no data, then this value must be zero.
/// </summary> /// </summary>
public ushort Data { get { return _data; } set { _data = Data; } } public ushort Data { get { return _data; } internal set { _data = Data; } }
} }
/// <summary> /// <summary>

View File

@@ -72,6 +72,7 @@
</Compile> </Compile>
<Compile Include="Properties\VersionInfo.cs" /> <Compile Include="Properties\VersionInfo.cs" />
<Compile Include="TentativeStateCommitable.cs" /> <Compile Include="TentativeStateCommitable.cs" />
<Compile Include="TransferErrorEventArgs.cs" />
<Compile Include="TransferReadyEventArgs.cs" /> <Compile Include="TransferReadyEventArgs.cs" />
<Compile Include="Triplets\DGControl\DGControl.Callback2.cs" /> <Compile Include="Triplets\DGControl\DGControl.Callback2.cs" />
<Compile Include="Triplets\DGImage\DGImage.Filter.cs" /> <Compile Include="Triplets\DGImage\DGImage.Filter.cs" />

View File

@@ -455,6 +455,11 @@ namespace NTwain
/// </summary> /// </summary>
public event EventHandler<DataTransferredEventArgs> DataTransferred; public event EventHandler<DataTransferredEventArgs> DataTransferred;
/// <summary>
/// Occurs when an error has been encountered during transfer.
/// </summary>
public event EventHandler<TransferErrorEventArgs> TransferError;
/// <summary> /// <summary>
/// Called when <see cref="State"/> changed /// Called when <see cref="State"/> changed
/// and raises the <see cref="StateChanged" /> event. /// and raises the <see cref="StateChanged" /> event.
@@ -557,6 +562,22 @@ namespace NTwain
} }
} }
/// <summary>
/// Raises the <see cref="E:TransferError" /> event.
/// </summary>
/// <param name="e">The <see cref="TransferErrorEventArgs"/> instance containing the event data.</param>
protected virtual void OnTransferError(TransferErrorEventArgs e)
{
var hand = TransferError;
if (hand != null)
{
try
{
hand(this, e);
}
catch { }
}
}
#endregion #endregion
#region TWAIN logic during xfer work #region TWAIN logic during xfer work
@@ -780,6 +801,14 @@ namespace NTwain
} }
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr }); OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr });
} }
else
{
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
}
}
catch (Exception ex)
{
OnTransferError(new TransferErrorEventArgs { Exception = ex });
} }
finally finally
{ {
@@ -812,6 +841,10 @@ namespace NTwain
{ {
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath }); OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath });
} }
else
{
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
}
} }
#endregion #endregion
@@ -839,6 +872,14 @@ namespace NTwain
} }
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, ImageInfo = imgInfo }); OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr, ImageInfo = imgInfo });
} }
else
{
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
}
}
catch (Exception ex)
{
OnTransferError(new TransferErrorEventArgs { Exception = ex });
} }
finally finally
{ {
@@ -876,6 +917,10 @@ namespace NTwain
} }
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath, ImageInfo = imgInfo }); OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath, ImageInfo = imgInfo });
} }
else
{
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
}
} }
private void DoImageMemoryXfer() private void DoImageMemoryXfer()
@@ -953,11 +998,14 @@ namespace NTwain
} }
else else
{ {
// todo: provide better mechanism for failed xfer, like event handler OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
throw new TwainException("Failed to transfer data.");
} }
} }
} }
catch (Exception ex)
{
OnTransferError(new TransferErrorEventArgs { Exception = ex });
}
finally finally
{ {
State = 6; State = 6;
@@ -1074,6 +1122,14 @@ namespace NTwain
} }
File.Move(tempFile, finalFile); File.Move(tempFile, finalFile);
} }
else
{
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
}
}
catch (Exception ex)
{
OnTransferError(new TransferErrorEventArgs { Exception = ex });
} }
finally finally
{ {