Wrap event calls in try-catch to prevent consumers from breaking flow.

This commit is contained in:
Eugene Wang
2023-04-06 07:18:10 -04:00
parent e4a6d8d8d3
commit f7a3cc699e
5 changed files with 46 additions and 22 deletions

View File

@@ -52,7 +52,7 @@ namespace NTwain
public STS GetCapCurrent(CAP cap, out TW_CAPABILITY value)
{
value = new TW_CAPABILITY(cap);
return WrapInSTS(DGControl.Capability.Get(ref _appIdentity, ref _currentDS, ref value));
return WrapInSTS(DGControl.Capability.GetCurrent(ref _appIdentity, ref _currentDS, ref value));
}
/// <summary>

View File

@@ -31,8 +31,12 @@ namespace NTwain
internal set
{
_currentDS = value;
try
{
CurrentSourceChanged?.Invoke(this, value);
}
catch { }
}
}
TW_IDENTITY_LEGACY _currentDS;
@@ -57,8 +61,12 @@ namespace NTwain
if (_state != value)
{
_state = value;
try
{
StateChanged?.Invoke(this, value); // TODO: should care about thread
}
catch { }
}
}
}
STATE _state = STATE.S1;

View File

@@ -33,8 +33,12 @@ namespace NTwain
if (rc == TWRC.SUCCESS)
{
_defaultDS = ds;
try
{
DefaultSourceChanged?.Invoke(this, ds);
}
catch { }
}
return WrapInSTS(rc);
}
@@ -82,8 +86,12 @@ namespace NTwain
if (rc == TWRC.SUCCESS)
{
_defaultDS = source;
try
{
DefaultSourceChanged?.Invoke(this, source);
}
catch { }
}
return WrapInSTS(rc);
}

View File

@@ -31,7 +31,7 @@ namespace NTwain
/// </summary>
void EnterTransferRoutine()
{
// default options if source don't support them or whatever
// default options if source doesn't support changing them or whatever
bool xferImage = true;
bool xferAudio = false;
var imgXferMech = TWSX.NATIVE;
@@ -54,21 +54,13 @@ namespace NTwain
}
}
if (xferImage)
if (xferImage && GetCapCurrent(CAP.ICAP_XFERMECH, out TW_CAPABILITY cap).RC == TWRC.SUCCESS)
{
if (GetCapCurrent(CAP.ICAP_XFERMECH, out TW_CAPABILITY cap).RC == TWRC.SUCCESS)
{
// todo:
cap.Free(this);
imgXferMech = cap.ReadOneValue<TWSX>(this);
}
}
else if (xferAudio)
else if (xferAudio && GetCapCurrent(CAP.ACAP_XFERMECH, out cap).RC == TWRC.SUCCESS)
{
if (GetCapCurrent(CAP.ACAP_XFERMECH, out TW_CAPABILITY cap).RC == TWRC.SUCCESS)
{
// todo:
cap.Free(this);
}
audXferMech = cap.ReadOneValue<TWSX>(this);
}
TW_PENDINGXFERS pending = default;
@@ -79,8 +71,12 @@ namespace NTwain
{
var readyArgs = new TransferReadyEventArgs(this, pending.Count, (TWEJ)pending.EOJ);
_uiThreadMarshaller.Invoke(() =>
{
try
{
TransferReady?.Invoke(this, readyArgs);
}
catch { } // don't let consumer kill the loop if they have exception
});
switch (readyArgs.Cancel)

View File

@@ -134,8 +134,12 @@ namespace NTwain
if (DGControl.DeviceEvent.Get(ref _appIdentity, ref _currentDS, out TW_DEVICEEVENT de) == TWRC.SUCCESS)
{
_uiThreadMarshaller.BeginInvoke(() =>
{
try
{
DeviceEvent?.Invoke(this, de);
}
catch { }
});
}
break;
@@ -197,8 +201,12 @@ namespace NTwain
if (DGControl.Identity.GetDefault(ref _appIdentity, out TW_IDENTITY_LEGACY ds) == TWRC.SUCCESS)
{
_defaultDS = ds;
try
{
DefaultSourceChanged?.Invoke(this, _defaultDS);
}
catch { }
}
// determine memory mgmt routines used
if (((DG)AppIdentity.SupportedGroups & DG.DSM2) == DG.DSM2)
@@ -222,7 +230,11 @@ namespace NTwain
State = STATE.S2;
_entryPoint = default;
_defaultDS = default;
try
{
DefaultSourceChanged?.Invoke(this, _defaultDS);
}
catch { }
_hwnd = IntPtr.Zero;
}
return WrapInSTS(rc, true);