mirror of
https://github.com/soukoku/ntwain.git
synced 2025-10-24 18:29:04 +08:00
Updated winform sample with sync context usage.
This commit is contained in:
@@ -39,6 +39,13 @@ namespace Tester.Winform
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected override void OnHandleCreated(EventArgs e)
|
||||||
|
{
|
||||||
|
base.OnHandleCreated(e);
|
||||||
|
SetupTwain();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnFormClosing(FormClosingEventArgs e)
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||||
{
|
{
|
||||||
if (_twain != null)
|
if (_twain != null)
|
||||||
@@ -59,16 +66,13 @@ namespace Tester.Winform
|
|||||||
{
|
{
|
||||||
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
|
var appId = TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetEntryAssembly());
|
||||||
_twain = new TwainSession(appId);
|
_twain = new TwainSession(appId);
|
||||||
// either set this and don't worry about threads during events,
|
|
||||||
// or don't and invoke during the events yourself
|
|
||||||
//_twain.SynchronizationContext = SynchronizationContext.Current;
|
|
||||||
_twain.StateChanged += (s, e) =>
|
_twain.StateChanged += (s, e) =>
|
||||||
{
|
{
|
||||||
Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId);
|
Debug.WriteLine("State changed to " + _twain.State + " on thread " + Thread.CurrentThread.ManagedThreadId);
|
||||||
};
|
};
|
||||||
_twain.DataTransferred += (s, e) =>
|
_twain.DataTransferred += (s, e) =>
|
||||||
{
|
{
|
||||||
Debug.WriteLine("Transferred data.");
|
Debug.WriteLine("Transferred data event on thread " + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
|
||||||
// example on getting ext image info
|
// example on getting ext image info
|
||||||
var infos = e.GetExtImageInfo(ExtendedImageInfo.Camera).Where(it => it.ReturnCode == ReturnCode.Success);
|
var infos = e.GetExtImageInfo(ExtendedImageInfo.Camera).Where(it => it.ReturnCode == ReturnCode.Success);
|
||||||
@@ -104,6 +108,7 @@ namespace Tester.Winform
|
|||||||
};
|
};
|
||||||
_twain.SourceDisabled += (s, e) =>
|
_twain.SourceDisabled += (s, e) =>
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("Source disabled event on thread " + Thread.CurrentThread.ManagedThreadId);
|
||||||
this.BeginInvoke(new Action(() =>
|
this.BeginInvoke(new Action(() =>
|
||||||
{
|
{
|
||||||
btnStopScan.Enabled = false;
|
btnStopScan.Enabled = false;
|
||||||
@@ -114,8 +119,21 @@ namespace Tester.Winform
|
|||||||
};
|
};
|
||||||
_twain.TransferReady += (s, e) =>
|
_twain.TransferReady += (s, e) =>
|
||||||
{
|
{
|
||||||
|
Debug.WriteLine("Transferr ready event on thread " + Thread.CurrentThread.ManagedThreadId);
|
||||||
e.CancelAll = _stopScan;
|
e.CancelAll = _stopScan;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// either set sync context and don't worry about threads during events,
|
||||||
|
// or don't and use control.invoke during the events yourself
|
||||||
|
Debug.WriteLine("Setup thread = " + Thread.CurrentThread.ManagedThreadId);
|
||||||
|
_twain.SynchronizationContext = SynchronizationContext.Current;
|
||||||
|
if (_twain.State < 3)
|
||||||
|
{
|
||||||
|
// use this for internal msg loop
|
||||||
|
_twain.Open();
|
||||||
|
// use this to hook into current app loop
|
||||||
|
//_twain.Open(new WindowsFormsMessageLoopHook(this.Handle));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CleanupTwain()
|
private void CleanupTwain()
|
||||||
@@ -153,6 +171,27 @@ namespace Tester.Winform
|
|||||||
ReloadSourceList();
|
ReloadSourceList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ReloadSourceList()
|
||||||
|
{
|
||||||
|
if (_twain.State >= 3)
|
||||||
|
{
|
||||||
|
while (btnSources.DropDownItems.IndexOf(sepSourceList) > 0)
|
||||||
|
{
|
||||||
|
var first = btnSources.DropDownItems[0];
|
||||||
|
first.Click -= SourceMenuItem_Click;
|
||||||
|
btnSources.DropDownItems.Remove(first);
|
||||||
|
}
|
||||||
|
foreach (var src in _twain)
|
||||||
|
{
|
||||||
|
var srcBtn = new ToolStripMenuItem(src.Name);
|
||||||
|
srcBtn.Tag = src;
|
||||||
|
srcBtn.Click += SourceMenuItem_Click;
|
||||||
|
srcBtn.Checked = _twain.CurrentSource != null && _twain.CurrentSource.Name == src.Name;
|
||||||
|
btnSources.DropDownItems.Insert(0, srcBtn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SourceMenuItem_Click(object sender, EventArgs e)
|
void SourceMenuItem_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
// do nothing if source is enabled
|
// do nothing if source is enabled
|
||||||
@@ -244,44 +283,9 @@ namespace Tester.Winform
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region real work
|
|
||||||
|
|
||||||
private void ReloadSourceList()
|
|
||||||
{
|
|
||||||
if (_twain == null)
|
|
||||||
{
|
|
||||||
SetupTwain();
|
|
||||||
}
|
|
||||||
if (_twain.State < 3)
|
|
||||||
{
|
|
||||||
// use this for internal msg loop
|
|
||||||
_twain.Open();
|
|
||||||
// use this to hook into current app loop
|
|
||||||
//_twain.Open(new WindowsFormsMessageLoopHook(this.Handle));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_twain.State >= 3)
|
|
||||||
{
|
|
||||||
while (btnSources.DropDownItems.IndexOf(sepSourceList) > 0)
|
|
||||||
{
|
|
||||||
var first = btnSources.DropDownItems[0];
|
|
||||||
first.Click -= SourceMenuItem_Click;
|
|
||||||
btnSources.DropDownItems.Remove(first);
|
|
||||||
}
|
|
||||||
foreach (var src in _twain)
|
|
||||||
{
|
|
||||||
var srcBtn = new ToolStripMenuItem(src.Name);
|
|
||||||
srcBtn.Tag = src;
|
|
||||||
srcBtn.Click += SourceMenuItem_Click;
|
|
||||||
srcBtn.Checked = _twain.CurrentSource != null && _twain.CurrentSource.Name == src.Name;
|
|
||||||
btnSources.DropDownItems.Insert(0, srcBtn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#region cap control
|
#region cap control
|
||||||
|
|
||||||
|
|
||||||
@@ -405,7 +409,5 @@ namespace Tester.Winform
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user