Skip to main content

Downloading a file : TaskCompleteSource approach






string dowloadFilePath = @"D:\InstanceType.json";
            Task<bool> task = DownloadPriceListCsvFromAWS("https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/us-gov-east-1/index.json", dowloadFilePath);
            task.Wait(60000);
            if (task.IsCompleted)
            {
                Console.WriteLine("File download compalete {0}", task.Result);
            }
            else
            {
                Console.WriteLine("Download does not seem to be completed within the specified time");
                WebClient webClient = (WebClient)task.AsyncState;
                webClient.CancelAsync();
                bool bRet = task.Result;
                if (File.Exists(dowloadFilePath))
                {
                    Console.WriteLine("Partially downloaded file exist. Cleaning it up");
                    File.Delete(dowloadFilePath);
                }
            }

 private static Task<bool> DownloadPriceListCsvFromAWS(string url, string downloadfilepath)
        {
            System.Net.WebClient webClinet = new System.Net.WebClient();
            TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(webClinet);
            webClinet.DownloadFileCompleted += (SendOrPostCallback, eventArgs) =>
            {
                if (eventArgs.Cancelled || eventArgs.Error != null)
                {
                    Console.WriteLine("Download operation either cancelled or an error occured");
                    tcs.SetResult(false);
                }
                else {
                    Console.WriteLine("Completed downloading the file");
                    tcs.SetResult(true);
                }
            };
            webClinet.DownloadFileAsync(new Uri(url), downloadfilepath);
            return tcs.Task;
        }

Comments

Popular posts from this blog

How to configure Microsoft SQL Server in EAP/Wildfly

Wednesday, December 11, 2019 9:19 AM Download the appropriate jdbc driver from https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15 Extract the driver package to the modules folder under EAP Here is from where my EAP is running C:\Users\admin\EAP-7.0.0\ Create a folder named "sqlserver" under C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft Copy the extracted driver jar file (for example mssql-jdbc-6.4.0.jre8.jar) to C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft\main Now create a modules.xml file in   C:\Users\admin\EAP-7.0.0\modules\system\layers\base\com\microsoft\main with the following settings <?xml version="1.0" encoding="UTF-8"?> <module xmlns="urn:jboss:module:1.3" name="com.microsoft.sqlserver">     <resources>         <reso...

How to find locked binaries as part of upgrade/fresh installation?

How to find locked binaries as part of upgrade/fresh installation? When you upgrade an application using windows installer many a time you might have come across issues like the files which you wanted to overwrite or delete is already in use or in another way some other application is already using that binary. In such cases, the windows installer will show a FilesInUse dialog. However, the problem here is this dialog will show only the application name which is consuming the binary and not the actual binary name. This has 2 problems, in case of a simple application which a handful of binaries we can easily figure out the binary getting locked, however in case of a large application with several binaries and run times it might be tricky to find out such locked binaries. The problem gets even more complicated if this scenario occurs in an environment where you don't have access, for example, a customer environment. Let me briefly explain how installer identifies and shows th...

Base64 Encoding

The base-64 encoding converts a series of arbitrary bytes into a longer sequence of common text characters that are all legal header field values. Base-64 encoding takes a sequence of 8-bit bytes, breaks the sequence into 6-bit pieces, and assigns each 6-bit piece to one of 64 characters comprising the base-64 alphabet. Base 64–encoded strings are about 33% larger than the original values. For example “Ow!” -> “T3ch” 1. The string “Ow!” is broken into 3 8-bit bytes (0x4F, 0x77, 0x21). 2. The 3 bytes create the 24-bit binary value 010011110111011100100001. 3. These bits are segmented into the 6-bit sequences 010011, 110111, 01110,100001.