Skip to main content

Asynchronous Methods

Every operation that touches the file system or runs text recognition comes in both a synchronous and an asynchronous flavour. The …Async variant does the work on a background thread, which keeps your UI responsive.

tip

We recommend using the asynchronous variants, since a synchronous call occupies the thread it is called on. Calling them from your UI thread keeps your app responsive at all times.

SynchronousAsynchronous
Document.LoadFileDocument.LoadFileAsync
Document.WritePDFDocument.WritePDFAsync
Document.WriteImageDocument.WriteImageAsync
Document.GetImageBytesDocument.GetImageBytesAsync
DocumentDataReader.LoadFileDocumentDataReader.LoadFileAsync
DocumentDataReader.GetTextDocumentDataReader.GetTextAsync
DocumentDataReader.AnalyzeDocumentDataReader.AnalyzeAsync
DocumentDataReader.SetAnalyzeConfigurationDocumentDataReader.SetAnalyzeConfigurationAsync
DocutainSDK.DeleteTempFilesDocutainSDK.DeleteTempFilesAsync

Both variants take the same parameters, return the same values and report errors the same way, see Error Handling.

using Docutain.SDK.MAUI;

//...
//scan or import a file
//...

var destinationPath = Path.Combine(FileSystem.CacheDirectory, "TestPDF.pdf");

//synchronous, runs on the calling thread
var pdfFile = Document.WritePDF(destinationPath);

//asynchronous, runs on a background thread
var pdfFileAsync = await Document.WritePDFAsync(destinationPath);
info

UI.ScanDocument and UI.StartPhotoPayment are asynchronous by nature, since they wait for the user to finish. They have no synchronous counterpart.

Argument validation

The …Async variants validate their arguments and the initialization state before the background work is started. That means an ArgumentException or a DocutainSdkNotInitializedException is thrown by the call itself and not raised through the returned Task, so a try/catch around the await catches both cases.