Xamarin Form - TaskCanceledException thrown on ScanFilesToFolderAsync

亡梦爱人 提交于 2019-12-22 12:36:51

问题


in my UWP application i am working on scan functionality. in this application user can scan the document through scanner by selecting flatbed or auto feeder.Now problem is when i am trying to scan it gives the en exception a Task was canceled.

please help..

thanks in advance. :)

have a great day... :)

private async void Btnscan_Click(object sender, RoutedEventArgs e)
    {

        FolderPicker folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        folderPicker.FileTypeFilter.Add("*");

        StorageFolder folder = await folderPicker.PickSingleFolderAsync();
//set the destination folder name for scan images.


        DeviceInformationDisplay selectedScanner = CmbScannerList.SelectedItem as DeviceInformationDisplay; // here i got the selected scanner.

        // scanner id is := "\\\\?\\ROOT#IMAGE#0000#{6bdd1fc6-810f-11d0-bec7-08002be2092f}"

        ScanToFolder(selectedScanner.id, folder);


    }

function Scan To folder

 public async void ScanToFolder(string deviceId, StorageFolder folder)
    {
        try
        {
 cancellationToken = new CancellationTokenSource();

            ImageScanner myScanner = await ImageScanner.FromIdAsync(deviceId);

            if (myScanner.IsScanSourceSupported(ImageScannerScanSource.Flatbed))
            {
                var result = await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Flatbed, folder).AsTask(cancellationToken.Token); // through an exception(A Task Was Canceled):(
Utils.DisplayImageAndScanCompleteMessage(result.ScannedFiles, DisplayImage);

            }

        }

        catch (Exception ex)
        {
            // here i got the exception.
        }

    }

Updated :

Now i am set the DeviceClass to ALL.

   private void StartWatcher()
    {
        resultCollection.Clear();
        DeviceWatcher deviceWatcher;

        deviceWatcher = DeviceInformation.CreateWatcher(DeviceClass.All); // set Image scanner to all. 
       deviceWatcherHelper.StartWatcher(deviceWatcher);
  }

After run the project in scanner list i got the all connected devices in which i got my scanner name This : when i am trying to pass this name it gives error in imagescanner System.Exception: 'Exception from HRESULT: 0x80210015' means device not found. Now i am chnage all to ImageScanner i got nothing in scanner list.

and in scanner HP application i got this name. and IT Scan Well :( in scanner list i don't got this name in my application. :(

on my pc setting -> devices -> scanner and printers i got those name.


回答1:


Rewriting the resolution of the problem as an answer. I tested the code on my machine where it behaved correctly and argued the problem is most likely a driver issue. This has been confirmed by the OP and reinstall of the driver helped make the scanning work again.



来源:https://stackoverflow.com/questions/55686040/xamarin-form-taskcanceledexception-thrown-on-scanfilestofolderasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!