//Allows for concurrent downloading of files
        //Allows for concurrent downloading of files
        private static async void downloadFile(QueuedFile nextFile)
        {
            testClient       = new WebClient();
            testClient.Proxy = GlobalProxySelection.GetEmptyWebProxy();
            try {
                /**Stream stream = null;
                 * HttpWebRequest ElementRequest = (HttpWebRequest)WebRequest.Create(nextFile.url);
                 * ElementRequest.UserAgent = "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
                 * ElementRequest.Referer = "http://google.com";
                 * HttpWebResponse HTMLResponse = (HttpWebResponse)ElementRequest.GetResponse();
                 * Stream streamResponse = HTMLResponse.GetResponseStream();
                 * //Downloads the image and saves it to the memorystream
                 * stream = HTMLResponse.GetResponseStream();
                 * nextFile.stream = stream;**/
                //Attempting to fix 403 error on many fullsize image loads
                byte[] testBytes = await testClient.DownloadDataTaskAsync(new Uri(nextFile.url));

                nextFile.stream = new MemoryStream(testBytes);
                //ImageDisplay.printImage(stream);
                saveFile(nextFile);
            } catch (Exception e) {
                CU.WCol(CU.nl + "Could not download image at url " + nextFile.url + " : " + e + CU.nl, CU.r, CU.y);
                CU.WCol(CU.nl + e.StackTrace + CU.nl, CU.r, CU.y);
            }
        }
 public static void saveFile(QueuedFile nextFile)
 {
     using (fileStream = new FileStream(nextFile.fileInfo.FullName, nextFile.fileMode)) {
         nextFile.stream.CopyTo(fileStream);
         fileStream.Flush();
         nextFile.stream.Flush();
         //CU.WCol(CU.nl + "Saved " + nextFile.fileInfo.FullName + CU.nl, CU.c);
     }
 }
        private static async void SaveFiles()
        {
            Queue <Task> downloadTasks = new Queue <Task>();

            while (true)
            {
                if (queueStack.Count > 0)
                {
                    QueuedFile nextFile = queueStack.Dequeue();
                    if (nextFile.fileInfo == null)
                    {
                        continue;
                    }
                    //Checks if the next file contains URL info
                    if (nextFile.url != null)
                    {
                        Task newDownload = downloadThreadFactory.StartNew(() => downloadFile(nextFile));
                        downloadTasks.Enqueue(newDownload);
                    }
                    else
                    {
                        saveFile(nextFile);
                        //Print image preview to console
                    }
                }
                while (downloadTasks.Count > 0)
                {
                    Task finishedTask = downloadTasks.Dequeue();
                    finishedTask.Wait();
                }
                //Ded
                if (shouldAnHero)
                {
                    return;
                }
            }
        }
 //Adds files to the queue
 public void AddToQueue(QueuedFile newFile)
 {
     queueStack.Enqueue(newFile);
 }