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
// 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;
FileInfo = fileInfo;
}
public DataTransferredEventArgs(TW_AUDIOINFO info, BufferedData data)
public TransferredEventArgs(TW_AUDIOINFO info, BufferedData data)
{
AudioInfo = info;
_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;
FileInfo = fileInfo;
IsImage = true;
_data = data;
_twain = twain;
}
TwainAppSession? _twain;
/// <summary>
/// Whether transferred data is an image or audio.
/// </summary>
@ -57,5 +60,18 @@ namespace NTwain
/// </summary>
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
{
get => _appIdentity;
internal set
{
_appIdentity = value;
}
}
TW_IDENTITY_LEGACY _appIdentity;
@ -28,7 +24,7 @@ namespace NTwain
public TW_IDENTITY_LEGACY CurrentSource
{
get => _currentDS;
internal set
protected set
{
_currentDS = value;
try
@ -56,16 +52,22 @@ namespace NTwain
public STATE State
{
get => _state;
internal set
protected set
{
if (_state != value)
{
_state = value;
if (StateChanged != null)
{
_uiThreadMarshaller.Invoke(() =>
{
try
{
StateChanged?.Invoke(this, value); // TODO: should care about thread
StateChanged.Invoke(this, value);
}
catch { }
});
}
}
}
}
@ -126,7 +128,6 @@ namespace NTwain
/// <summary>
/// Fires when <see cref="State"/> changes.
/// This is not guaranteed to be raised on the UI thread.
/// </summary>
public event TwainEventDelegate<STATE>? StateChanged;
@ -163,7 +164,8 @@ namespace NTwain
/// <summary>
/// Fires when transferred data is available for app to use.
/// This is NOT raised on the UI thread for reasons.
/// </summary>
public event TwainEventDelegate<DataTransferredEventArgs>? DataTransferred;
public event TwainEventDelegate<TransferredEventArgs>? Transferred;
}
}

View File

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

View File

@ -133,23 +133,23 @@ namespace NTwain
});
break;
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(() =>
{
try
{
DeviceEvent?.Invoke(this, de);
DeviceEvent.Invoke(this, de);
}
catch { }
});
}
break;
case MSG.XFERREADY:
_uiThreadMarshaller.Invoke(() =>
{
//_uiThreadMarshaller.Invoke(() =>
//{
State = STATE.S6;
});
//});
EnterTransferRoutine();
break;
}