Search code examples
c#xamarin.formsuwpwin-universal-app

Xamarin Form - TaskCanceledException thrown on ScanFilesToFolderAsync


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.

enter image description here

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. :( enter image description here

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

enter image description here


Solution

  • 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.