简体   繁体   English

防止扫描仪冻结程序

[英]Prevent scanner from freezing program

I'm currently developing a module where a user can scan documents and save them. 我目前正在开发一个模块,用户可以在其中扫描文档并将其保存。 currently, the working code that is being used is from Jeske , from GitHub (link is here ). 当前,正在使用的工作代码来自GitHub的Jeske (链接在此处 )。 The scanner that the customer is using is : Epson Perfection V600 . 客户使用的扫描仪是: Epson Perfection V600 The earlier mentioned code is working like a charm, expect for 1 situation: If the scanner goes into sleep/inactive mode and the user tries to scan a document while in that state, the application will freeze itself while waiting for the scanner to respond ( sometimes it does and sometimes it doesn't). 前面提到的代码可以正常运行,可预期出现以下一种情况:如果扫描仪进入睡眠/非活动模式,并且用户在该状态下尝试扫描文档,则应用程序将在等待扫描仪响应时冻结自身(有时会,有时却不会)。 To be exact, the program freezes at this particular line: 确切地说,程序冻结在以下特定行:

WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);

My question is: is there a way to prevent the application from freezing while trying to scan from the given scanner?(This issue only happens when scanner is in sleep/inactive mode) 我的问题是:有没有办法防止应用程序在尝试从给定的扫描仪进行扫描时冻结?(仅当扫描仪处于睡眠/非活动模式时,才会发生此问题)

you can use concurrency to prevent blocking. 您可以使用并发来防止阻塞。 If you also use CancellationToken with CancellationTokenSource whenever the operation does not ends, you can send cancel signal to your funtion so the scanning operation will be aborted. 如果每当操作没有结束时也将CancellationToken与CancellationTokenSource一起使用,则可以向函数发送cancel信号,以便扫描操作将中止。

public Task Scan()
{
    ....
    WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);
    ...
}

then in your main program 然后在您的主程序中

//wait 20 seconds before cancellation
CancellationTokenSource cts=  new CancellationTokenSource(20000);
var myTask = Task.Run(() => Scan(),  cts.Token);
await myTask;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM