mirror of
https://github.com/soukoku/ntwain.git
synced 2025-09-18 17:47:57 +08:00
Added xfer error event instead of throwing exceptions during xfers.
This commit is contained in:
@@ -455,6 +455,11 @@ namespace NTwain
|
||||
/// </summary>
|
||||
public event EventHandler<DataTransferredEventArgs> DataTransferred;
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when an error has been encountered during transfer.
|
||||
/// </summary>
|
||||
public event EventHandler<TransferErrorEventArgs> TransferError;
|
||||
|
||||
/// <summary>
|
||||
/// Called when <see cref="State"/> changed
|
||||
/// 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
|
||||
|
||||
#region TWAIN logic during xfer work
|
||||
@@ -780,6 +801,14 @@ namespace NTwain
|
||||
}
|
||||
OnDataTransferred(new DataTransferredEventArgs { NativeData = lockedPtr });
|
||||
}
|
||||
else
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { Exception = ex });
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -812,6 +841,10 @@ namespace NTwain
|
||||
{
|
||||
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath });
|
||||
}
|
||||
else
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -839,6 +872,14 @@ namespace NTwain
|
||||
}
|
||||
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
|
||||
{
|
||||
@@ -876,6 +917,10 @@ namespace NTwain
|
||||
}
|
||||
OnDataTransferred(new DataTransferredEventArgs { FileDataPath = filePath, ImageInfo = imgInfo });
|
||||
}
|
||||
else
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
|
||||
}
|
||||
}
|
||||
|
||||
private void DoImageMemoryXfer()
|
||||
@@ -953,11 +998,14 @@ namespace NTwain
|
||||
}
|
||||
else
|
||||
{
|
||||
// todo: provide better mechanism for failed xfer, like event handler
|
||||
throw new TwainException("Failed to transfer data.");
|
||||
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { Exception = ex });
|
||||
}
|
||||
finally
|
||||
{
|
||||
State = 6;
|
||||
@@ -1074,6 +1122,14 @@ namespace NTwain
|
||||
}
|
||||
File.Move(tempFile, finalFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { ReturnCode = xrc, SourceStatus = this.GetSourceStatus() });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnTransferError(new TransferErrorEventArgs { Exception = ex });
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
Reference in New Issue
Block a user