saraff-9EB1047A4BEB4cef8506B29BA325BD5A / Saraff.Twain.NET

Saraff.Twain.NET is the skillful scanning component which allows you to control work of flatbed scanner, web and digital camera and any other TWAIN device from .NET environment. You can use this library in your programs written in any programming languages compatible with .NET technology.
GNU General Public License v3.0
102 stars 35 forks source link

Close TwainExternalProcess before completion #24

Closed MrBlueHere closed 6 years ago

MrBlueHere commented 6 years ago

Hi,

to explain my situation: I'm wrapping TwainExternalProcess.Execute method inside Task.Run and I do twain.OpenDataSource in newly created process. This way my app doesn't lock if device isn't online and when device driver displays it's own message/s on the screen and blocks the program flow. I wrap it inside task, so that I can wait certain timeout period for it to complete and if it doesn't, I throw a TimeoutException signaling that device wasn't connected in timely fashion and is probably offline. This has proven to me as a fairly good way to check if device is online.

My problem is that after creating these processes and doing twain.OpenDataSource, the driver UI messages stay on the screen waiting to be closed by the user. This behavior isn't wanted.

A possible solution I see is to close those TwainExternalProcesses I created and thereby also closing driver-generated messages. How could I do that? I see, that in AuxProcess is a method End(), which is what I need, but it's not accessible from outside.

I know that I could look for these processes with System.Management, but I didn't really want to do this, as I don't know how to identify each specific TwainExternalProcess (in case there are more devices offline).

Thank you and have a nice day.

saraff-9EB1047A4BEB4cef8506B29BA325BD5A commented 6 years ago

Hello, @MrBlueHere I has changed code of a Saraff.Twain.Aux (you can see that in a commit). Now you can write follow:

IDisposable _host = null;
TwainExternalProcess.Execute (
    "path_of_host_file",
    (twain, host) => {
        _host = host;
        // ...
})
MrBlueHere commented 6 years ago

Wow, thanks. I'll try it out.

MrBlueHere commented 6 years ago

I was trying to test it out, but I found that I'm using older version of Saraff.Twain -> 1.0.22.544. After updating to 1.0.27.606, behavior changed when doing outproc operations. It doesn't matter which version of Saraff.Twain.Aux I use, the issue is still there. I can start TwainExternalProcess, but when calling twain.SourcesCount, this error comes up:

[5] Message: System.InvalidOperationException: Operation is not valid due to the current state of the object.

Server stack trace: at Saraff.Twain.Aux.TwainExternalProcess.AuxProcess.Execute(TwainCommand command) at Saraff.Twain.Aux.Twain32RealProxy.Invoke(IMessage msg)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Saraff.Twain.Twain32.get_SourcesCount() at ScanApp.Models.Scanner.<>cDisplayClass60_0.b_0(Twain32 twain) in D:\Projects\ScanApp\ScanApp\ScanApp\Models\Scanner.cs:line 120 at Saraff.Twain.Aux.TwainExternalProcess.<>cDisplayClass0_0.b_0(Twain32 twain, IDisposable host) at Saraff.Twain.Aux.TwainExternalProcess.Execute(String fileName, Action2 execCallback) at Saraff.Twain.Aux.TwainExternalProcess.Execute(String fileName, Action1 execCallback) at ScanApp.Models.Scanner.GetTwainScanners() in D:\Projects\ScanApp\ScanApp\ScanApp\Models\Scanner.cs:line 116

I'm not sure what's wrong, I looked at Saraff.Twain.NET Service Samples (UPDATED at 05.05.2017) and the way TwainExternalProcess is started is the same as my. Also I see that processes are created just fine but crash right away.

Edit: As this probably isn't related to this issue, I can create a new one if you want.

saraff-9EB1047A4BEB4cef8506B29BA325BD5A commented 6 years ago

In projects of external processes you similarly has updated version of Saraff.Twain ?

MrBlueHere commented 6 years ago

I forgot to update it, that was the problem, thanks. I've also tested out closing/disposing external process and it works as expected.

Thank you very much.

saraff-9EB1047A4BEB4cef8506B29BA325BD5A commented 6 years ago

Ok. I has published a Saraff.Twain.Aux 1.0.3.632 nuget package.