←Select platform

UseAsync Method

Summary

Creates the WebClient object used for asynchronous operations.

Syntax

C#
VB
C++
public void UseAsync() 
Public Sub UseAsync()  
public: 
void UseAsync();  

Remarks

The RasterCodecs object supports getting information, loading and saving image files asynchronously through the use of the GetInformationAsync, LoadAsync, LoadSvgAsync and SaveAsync methods. These methods will use an internal WebClient object to perform their operations and this object will be created automatically on the first call to these methods and can be accessed through the AsyncWebClient property.

If it is desired to create the WebClient object manually (for example, to subscribe to any of its event or set the credentials prior to invoking any read operations), you can call UseAsync to create the object, change its behavior and then call GetInformationAsync, LoadAsync, LoadSvgAsync or SaveAsync.

Note: UseAsync, AsyncWebClient, IsAsyncBusy, and CancelAsync are only applicable when the GetInformationAsync, LoadAsync, LoadSvgAsync or SaveAsync was with the versions that take a Uri parameter. The versions that take a String or a Stream do not use a WebClient object internally.

If you want to check the progress or cancel the operation then use the LoadImage event as normal.

Example

This example will use UseAsync to create the WebClient object used by a RasterCodecs object for asynchronous operations. It will then change the cache policy of the web client object before loading an image asynchronously.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
using LeadtoolsExamples.Common; 
 
public void UseAsyncExample() 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   Uri uri = new Uri(srcFileName); 
 
   RasterCodecs codecs = new RasterCodecs(); 
 
   // The RasterCodecs.AsyncWebClient is null first 
   Debug.Assert(codecs.AsyncWebClient == null); 
 
   // Force the RasterCodecs object to create it 
   codecs.UseAsync(); 
 
   // Change the Cache policy 
   codecs.AsyncWebClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.CacheOnly); 
 
   // Now Load the file asynchronously 
   codecs.LoadAsyncCompleted += new EventHandler<CodecsLoadAsyncCompletedEventArgs>(useAsyncExample_LoadAsyncCompleted); 
   RasterImage image = codecs.LoadAsync(uri, null); 
} 
 
private void useAsyncExample_LoadAsyncCompleted(object sender, CodecsLoadAsyncCompletedEventArgs e) 
{ 
   if (e.Cancelled) 
   { 
      Console.WriteLine("User canceled"); 
   } 
   else if (e.Error != null) 
   { 
      Console.WriteLine("Error: " + e.Error.Message); 
   } 
   else 
   { 
      // Get the image 
      RasterImage image = e.Image; 
      Console.WriteLine("Image loaded asynchronously: Size = {0} by {1}", image.Width, image.Height); 
      image.Dispose(); 
   } 
 
   // Unsubscribe to the event and dispose the RasterCodecs object 
   RasterCodecs codecs = sender as RasterCodecs; 
   codecs.LoadAsyncCompleted -= new EventHandler<CodecsLoadAsyncCompletedEventArgs>(useAsyncExample_LoadAsyncCompleted); 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
Imports Leadtools.Drawing 
Imports Leadtools.Svg 
 
Public Sub UseAsyncExample() 
   Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp") 
   Dim uri As New Uri(srcFileName) 
 
   Dim codecs As New RasterCodecs() 
 
   ' The RasterCodecs.AsyncWebClient is Nothing first 
   Debug.Assert(Not IsNothing(codecs.AsyncWebClient)) 
 
   ' Force the RasterCodecs object to create it 
   codecs.UseAsync() 
 
   ' Change the Cache policy 
   codecs.AsyncWebClient.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.CacheOnly) 
 
   ' Now Load the file asynchronously 
   AddHandler codecs.LoadAsyncCompleted, AddressOf useAsyncExample_LoadAsyncCompleted 
   Dim image As RasterImage = codecs.LoadAsync(uri, Nothing) 
End Sub 
 
Private Sub useAsyncExample_LoadAsyncCompleted(ByVal sender As Object, ByVal e As CodecsLoadAsyncCompletedEventArgs) 
   If e.Cancelled Then 
      Console.WriteLine("User canceled") 
   ElseIf Not IsNothing(e.Error) Then 
      Console.WriteLine("Error: " + e.Error.Message) 
   Else 
      ' Get the image 
      Dim image As RasterImage = e.Image 
      Console.WriteLine("Image loaded asynchronously: Size = {0} by {1}", image.Width, image.Height) 
      image.Dispose() 
   End If 
 
   ' Unsubscribe to the event and dispose the RasterCodecs object 
   Dim codecs As RasterCodecs = DirectCast(sender, RasterCodecs) 
   RemoveHandler codecs.LoadAsyncCompleted, AddressOf useAsyncExample_LoadAsyncCompleted 
   codecs.Dispose() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
End Class 

Requirements

Target Platforms

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly