This question is a continuation of Using HttpClient to download asynchronous files .
2015/01/15 Edited to add to the room for multithreading - there is still a secret,
using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; namespace TestHttpClient2 { class Program { static string baseUrl = "http://real-chart.finance.yahoo.com/"; static string requestUrlFormat = "/table.csv?s={0}&d=0&e=1&f=2016&g=d&a=0&b=1&c=1901&ignore=.csv"; static void Main(string[] args) { var activeTaskList = new List<Task>(); string outputDirectory = "StockQuotes"; if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } while (true) { Console.WriteLine("Enter symbol or [ENTER] to exit:"); string symbol = Console.ReadLine(); if (string.IsNullOrEmpty(symbol)) { break; } Task downloadTask = DownloadDataForStockAsync(outputDirectory, symbol); if (TaskIsActive(downloadTask)) {
Q) Why am I getting this intermittent error (at the end of this output.) (I copy a long list of duplicate NES lines to the clipboard and paste them into the console to duplicate the problem)
Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: NES Enter symbol or [ENTER] to exit: IN - Thread: 18 lastTimestampedString: OUT - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_472_NES IN - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_472_NES OUT - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_473_NES IN - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_473_NES OUT - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_493_NES IN - Thread: 16 lastTimestampedString: 2015_01_15_11_19_44_493_NES OUT - Thread: 16 lastTimestampedString: 2015_01_15_11_19_44_494_NES IN - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_494_NES OUT - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_495_NES IN - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_495_NES IN - Thread: 16 lastTimestampedString: 2015_01_15_11_19_44_495_NES OUT - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_496_NES IN - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_495_NES OUT - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_496_NES IN - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_496_NES OUT - Thread: 16 lastTimestampedString: 2015_01_15_11_19_44_495_NES OUT - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_497_NES IN - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_497_NES OUT - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_523_NES IN - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_523_NES OUT - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_532_NES IN - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_532_NES OUT - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_533_NES IN - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_533_NES Exception on thread: 17: The process cannot access the file 'C:\Users\drogers\_code\Tests\TestHttpClient\TestHttpClient2\bin\Debug\StockQuot es\2015_01_15_11_19_44_495_NES.csv' because it is being used by another process. Exception on thread: 16: The process cannot access the file 'C:\Users\drogers\_code\Tests\TestHttpClient\TestHttpClient2\bin\Debug\StockQuot es\2015_01_15_11_19_44_496_NES.csv' because it is being used by another process. OUT - Thread: 18 lastTimestampedString: 2015_01_15_11_19_44_540_NES IN - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_540_NES IN - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_540_NES OUT - Thread: 17 lastTimestampedString: 2015_01_15_11_19_44_557_NES OUT - Thread: 19 lastTimestampedString: 2015_01_15_11_19_44_560_NES Exception on thread: 19: The process cannot access the file 'C:\Users\drogers\_code\Tests\TestHttpClient\TestHttpClient2\bin\Debug\StockQuot es\2015_01_15_11_19_44_560_NES.csv' because it is being used by another process.
I can avoid the problem of uncommenting line 126 and line 127 of comments, as in:
// This is an asynchronous world - lock the shared resource before using it! lock (dummy) //lock (lastTimestampedString)
Looking at il, the only difference in the code generated for FormatTimestampedString is
ldsfld string modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile) TestHttpClient2.Program::**lastTimestampedString**
vs
ldsfld string modreq([mscorlib]System.Runtime.CompilerServices.IsVolatile) TestHttpClient2.Program::**dummy**
source share