Renamed datatransferred event.

This commit is contained in:
Eugene Wang 2023-04-09 13:19:16 -04:00
parent 62c091fd12
commit 96a212a42b
4 changed files with 57 additions and 30 deletions

View File

@ -6,27 +6,30 @@ namespace NTwain
// TODO: maybe a 2-level "dispose" with end of event being 1 // TODO: maybe a 2-level "dispose" with end of event being 1
// and manual dispose 2 for perf if this is not good enough. // and manual dispose 2 for perf if this is not good enough.
public class DataTransferredEventArgs : EventArgs public class TransferredEventArgs : EventArgs
{ {
public DataTransferredEventArgs(TW_AUDIOINFO info, TW_SETUPFILEXFER fileInfo) public TransferredEventArgs(TW_AUDIOINFO info, TW_SETUPFILEXFER fileInfo)
{ {
AudioInfo = info; AudioInfo = info;
FileInfo = fileInfo; FileInfo = fileInfo;
} }
public DataTransferredEventArgs(TW_AUDIOINFO info, BufferedData data) public TransferredEventArgs(TW_AUDIOINFO info, BufferedData data)
{ {
AudioInfo = info; AudioInfo = info;
_data = data; _data = data;
} }
public DataTransferredEventArgs(TW_IMAGEINFO info, TW_SETUPFILEXFER? fileInfo, BufferedData data) public TransferredEventArgs(TwainAppSession twain, TW_IMAGEINFO info, TW_SETUPFILEXFER? fileInfo, BufferedData data)
{ {
ImageInfo = info; ImageInfo = info;
FileInfo = fileInfo; FileInfo = fileInfo;
IsImage = true; IsImage = true;
_data = data; _data = data;
_twain = twain;
} }
TwainAppSession? _twain;
/// <summary> /// <summary>
/// Whether transferred data is an image or audio. /// Whether transferred data is an image or audio.
/// </summary> /// </summary>
@ -57,5 +60,18 @@ namespace NTwain
/// </summary> /// </summary>
public TW_AUDIOINFO AudioInfo { get; } public TW_AUDIOINFO AudioInfo { get; }
/// <summary>
/// Gets the ext image info. Use any utility methods on it
/// to read the data. Remember to call <see cref="TW_EXTIMAGEINFO.Free(IMemoryManager)"/>
/// when done.
/// </summary>
/// <param name="container">Container to query. Can be created with <see cref="TW_EXTIMAGEINFO.CreateRequest(TWEI[])"/></param>
/// <returns></returns>
public STS GetExtendedImageInfo(ref TW_EXTIMAGEINFO container)
{
if (_twain == null) return default;
return _twain.GetExtendedImageInfo(ref container);
}
} }
} }

View File

@ -15,10 +15,6 @@ namespace NTwain
public TW_IDENTITY_LEGACY AppIdentity public TW_IDENTITY_LEGACY AppIdentity
{ {
get => _appIdentity; get => _appIdentity;
internal set
{
_appIdentity = value;
}
} }
TW_IDENTITY_LEGACY _appIdentity; TW_IDENTITY_LEGACY _appIdentity;
@ -28,7 +24,7 @@ namespace NTwain
public TW_IDENTITY_LEGACY CurrentSource public TW_IDENTITY_LEGACY CurrentSource
{ {
get => _currentDS; get => _currentDS;
internal set protected set
{ {
_currentDS = value; _currentDS = value;
try try
@ -56,16 +52,22 @@ namespace NTwain
public STATE State public STATE State
{ {
get => _state; get => _state;
internal set protected set
{ {
if (_state != value) if (_state != value)
{ {
_state = value; _state = value;
if (StateChanged != null)
{
_uiThreadMarshaller.Invoke(() =>
{
try try
{ {
StateChanged?.Invoke(this, value); // TODO: should care about thread StateChanged.Invoke(this, value);
} }
catch { } catch { }
});
}
} }
} }
} }
@ -126,7 +128,6 @@ namespace NTwain
/// <summary> /// <summary>
/// Fires when <see cref="State"/> changes. /// Fires when <see cref="State"/> changes.
/// This is not guaranteed to be raised on the UI thread.
/// </summary> /// </summary>
public event TwainEventDelegate<STATE>? StateChanged; public event TwainEventDelegate<STATE>? StateChanged;
@ -163,7 +164,8 @@ namespace NTwain
/// <summary> /// <summary>
/// Fires when transferred data is available for app to use. /// Fires when transferred data is available for app to use.
/// This is NOT raised on the UI thread for reasons.
/// </summary> /// </summary>
public event TwainEventDelegate<DataTransferredEventArgs>? DataTransferred; public event TwainEventDelegate<TransferredEventArgs>? Transferred;
} }
} }

View File

@ -67,14 +67,17 @@ namespace NTwain
do do
{ {
var readyArgs = new TransferReadyEventArgs(pending.Count, (TWEJ)pending.EOJ); var readyArgs = new TransferReadyEventArgs(pending.Count, (TWEJ)pending.EOJ);
if (TransferReady != null)
{
_uiThreadMarshaller.Invoke(() => _uiThreadMarshaller.Invoke(() =>
{ {
try try
{ {
TransferReady?.Invoke(this, readyArgs); TransferReady.Invoke(this, readyArgs);
} }
catch { } // don't let consumer kill the loop if they have exception catch { } // don't let consumer kill the loop if they have exception
}); });
}
if (readyArgs.Cancel == CancelType.EndNow || _closeDsRequested) if (readyArgs.Cancel == CancelType.EndNow || _closeDsRequested)
{ {
@ -108,7 +111,7 @@ namespace NTwain
try try
{ {
if (readyArgs.Cancel != CancelType.SkipCurrent && if (readyArgs.Cancel != CancelType.SkipCurrent &&
DataTransferred != null) Transferred != null)
{ {
// transfer normally and only if someone's listening // transfer normally and only if someone's listening
// to DataTransferred event // to DataTransferred event
@ -146,13 +149,19 @@ namespace NTwain
} }
catch (Exception ex) catch (Exception ex)
{ {
if (TransferError != null)
{
_uiThreadMarshaller.Invoke(() =>
{
try try
{ {
TransferError?.Invoke(this, new TransferErrorEventArgs(ex)); TransferError?.Invoke(this, new TransferErrorEventArgs(ex));
} }
catch { } catch { }
});
} }
} }
}
} while (sts.RC == TWRC.SUCCESS && pending.Count != 0); } while (sts.RC == TWRC.SUCCESS && pending.Count != 0);
} }
@ -229,8 +238,8 @@ namespace NTwain
try try
{ {
DGAudio.AudioInfo.Get(ref _appIdentity, ref _currentDS, out TW_AUDIOINFO info); DGAudio.AudioInfo.Get(ref _appIdentity, ref _currentDS, out TW_AUDIOINFO info);
var args = new DataTransferredEventArgs(info, fileSetup); var args = new TransferredEventArgs(info, fileSetup);
DataTransferred?.Invoke(this, args); Transferred?.Invoke(this, args);
} }
catch { } catch { }
; ;
@ -263,8 +272,8 @@ namespace NTwain
try try
{ {
DGAudio.AudioInfo.Get(ref _appIdentity, ref _currentDS, out TW_AUDIOINFO info); DGAudio.AudioInfo.Get(ref _appIdentity, ref _currentDS, out TW_AUDIOINFO info);
var args = new DataTransferredEventArgs(info, data); var args = new TransferredEventArgs(info, data);
DataTransferred?.Invoke(this, args); Transferred?.Invoke(this, args);
} }
catch { } catch { }
finally finally
@ -429,8 +438,8 @@ namespace NTwain
{ {
DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info); DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info);
// ToArray bypasses the XferMemPool but I guess this will have to do for now // ToArray bypasses the XferMemPool but I guess this will have to do for now
var args = new DataTransferredEventArgs(info, fileSetup, new BufferedData { Buffer = outStream.ToArray(), Length = (int)outStream.Length }); var args = new TransferredEventArgs(this, info, fileSetup, new BufferedData { Buffer = outStream.ToArray(), Length = (int)outStream.Length });
DataTransferred?.Invoke(this, args); Transferred?.Invoke(this, args);
} }
catch { } catch { }
@ -457,8 +466,8 @@ namespace NTwain
try try
{ {
DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info); DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info);
var args = new DataTransferredEventArgs(info, fileSetup, default); var args = new TransferredEventArgs(this, info, fileSetup, default);
DataTransferred?.Invoke(this, args); Transferred?.Invoke(this, args);
} }
catch { } catch { }
@ -503,8 +512,8 @@ namespace NTwain
try try
{ {
DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info); DGImage.ImageInfo.Get(ref _appIdentity, ref _currentDS, out TW_IMAGEINFO info);
var args = new DataTransferredEventArgs(info, null, data); var args = new TransferredEventArgs(this, info, null, data);
DataTransferred?.Invoke(this, args); Transferred?.Invoke(this, args);
} }
catch { } catch { }
finally finally

View File

@ -133,23 +133,23 @@ namespace NTwain
}); });
break; break;
case MSG.DEVICEEVENT: case MSG.DEVICEEVENT:
if (DGControl.DeviceEvent.Get(ref _appIdentity, ref _currentDS, out TW_DEVICEEVENT de) == TWRC.SUCCESS) if (DeviceEvent != null && DGControl.DeviceEvent.Get(ref _appIdentity, ref _currentDS, out TW_DEVICEEVENT de) == TWRC.SUCCESS)
{ {
_uiThreadMarshaller.BeginInvoke(() => _uiThreadMarshaller.BeginInvoke(() =>
{ {
try try
{ {
DeviceEvent?.Invoke(this, de); DeviceEvent.Invoke(this, de);
} }
catch { } catch { }
}); });
} }
break; break;
case MSG.XFERREADY: case MSG.XFERREADY:
_uiThreadMarshaller.Invoke(() => //_uiThreadMarshaller.Invoke(() =>
{ //{
State = STATE.S6; State = STATE.S6;
}); //});
EnterTransferRoutine(); EnterTransferRoutine();
break; break;
} }