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

Unicode and UTF8 Encoding

Unicode provides a unique number for every character, no matter what the platform, no matter what the program, no matter what the language. Unicode officially encodes 1,114,112 characters, from 0x000000 to 0x10FFFF. (The idea that Unicode is a 16-bit encoding is completely wrong.) For maximum compatibility, individual Unicode values are usually passed around as 32-bit integers (4 bytes per character), even though this is more than necessary. The consensus is that storing four bytes per character is wasteful, so a variety of representations have sprung up for Unicode characters. The most interesting one for C programmers is called UTF-8. UTF-8 is a "multi-byte" encoding scheme, meaning that it requires a variable number of bytes to represent a single Unicode value. Given a so-called "UTF-8 sequence", you can convert it to a Unicode value that refers to a character. http://www.cprogramming.com/tutorial/unicode.html There are 3 types of encoding in unicode, UT...

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

Docker in Linux

Docker Installation Need 64bit machine and follow the steps available in below link, https://docs.docker.com/installation/ubuntulinux/ What is Docker? Docker is a tool that promises to easily encapsulate the process of creating a distributable artifact for any application, deploying it at scale into any environment, and streamlining the workflow and responsiveness of agile software organizations. In a nutshell, here's what Docker can do for you: It can get more applications running on the same hardware than other technologies; it makes it easy for developers to quickly create, ready-to-run containered applications; and it makes managing and deploying applications much easier. Difference between hypervisor and containers The key difference between containers and VMs is that while the hypervisor abstracts an entire device, containers just abstract the operating system kernel. They are much more efficient than hypervisors in system resource terms. Instead of ...