Hi,
ich habe folgende Anwendung und Problem:
Anwendung fragt bei Server nach, ob Updates verfügbar, wenn ja, starte Download. Eine Datei nacheinander in einem DownloadHandler. Der DownloadHandler arbeitet mit AsyncResponse und so.
Problem: Wenn ich über die IDE starte (also Debug), läuft alles durch, beim Debuggen finde ich nichts. Es ging auch eigentlich vorher die ganze Zeit...
Wenn ich vom Explorer aus starte (also den Build, die exe nutze), lädt er eine Datei runter und fängt niemals mit der zweiten an.
Wenn ich ihn Sinnlos nach jedem Mist ne MSGBox ausgeben lasse, Arbeitet er solange, wie ich im Vordergrund Messageboxen weg klicke (Ich lasse ihn immer printen, wenn der Handler einen Download reinbekommt (also Methoden-Aufruf) und wenn er mit einem Download auch wirklich beginnt (Schleife im Hauptthread, welche quasi parallele Downloads via Boolean blockiert). Sobald ich langsamer Meldungen wegklicke, kommt irgendwann einfach nichts mehr...
Ich hab hier mal die Zuständige Klasse dafür...
Ich weiß nicht, welche Infos ihr gebrauchen könntet, ich weiß ja noch nicht einmal wo ich nachsuchen soll nach dem Fehler...
Irgendwer ne Idee oder Hilfestellung?
ich habe folgende Anwendung und Problem:
Anwendung fragt bei Server nach, ob Updates verfügbar, wenn ja, starte Download. Eine Datei nacheinander in einem DownloadHandler. Der DownloadHandler arbeitet mit AsyncResponse und so.
Problem: Wenn ich über die IDE starte (also Debug), läuft alles durch, beim Debuggen finde ich nichts. Es ging auch eigentlich vorher die ganze Zeit...
Wenn ich vom Explorer aus starte (also den Build, die exe nutze), lädt er eine Datei runter und fängt niemals mit der zweiten an.
Wenn ich ihn Sinnlos nach jedem Mist ne MSGBox ausgeben lasse, Arbeitet er solange, wie ich im Vordergrund Messageboxen weg klicke (Ich lasse ihn immer printen, wenn der Handler einen Download reinbekommt (also Methoden-Aufruf) und wenn er mit einem Download auch wirklich beginnt (Schleife im Hauptthread, welche quasi parallele Downloads via Boolean blockiert). Sobald ich langsamer Meldungen wegklicke, kommt irgendwann einfach nichts mehr...
Ich hab hier mal die Zuständige Klasse dafür...
Ich weiß nicht, welche Infos ihr gebrauchen könntet, ich weiß ja noch nicht einmal wo ich nachsuchen soll nach dem Fehler...
C#-Quellcode
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Net;
- using System.Windows.Forms;
- namespace kcUpdater.Classes
- {
- public class WebDownloader
- {
- private static readonly Lazy<WebDownloader> lazy = new Lazy<WebDownloader>(() => new WebDownloader());
- /// <summary>
- /// default useragend for web requests
- /// </summary>
- private static string _userAgent = "kcUpdate/0.3 (compatible; .NET4.0C; .NET4.0E; .NET4.5C; .NET4.5E; .NET4.6C; .NET4.6E)";
- /// <summary>
- /// Returns the Instance of this class
- /// </summary>
- public static WebDownloader Instance { get { return lazy.Value; } }
- public bool Downloading;
- public int FileNumber = 0;
- public int CurrentBytes;
- public int CurrentDownloadLength;
- public DBeginDownload BeginDownload;
- public DUpdateDownload UpdateDownload;
- public DEndDownload EndDownload;
- private string _target;
- private HttpWebRequest _request;
- private HttpWebResponse _response;
- private byte[] _dataBuffer;
- private FileStream _fileStream;
- /// <summary>
- /// Make the constructor private!
- /// </summary>
- private WebDownloader()
- {
- Downloading = false;
- }
- public void DownloadFile(Uri source, string target)
- {
- MessageBox.Show("Initiate " + source.AbsoluteUri);
- while (Downloading) { } //NOP - wait for current download to finnish
- MessageBox.Show("Ensure " + source.AbsoluteUri);
- string dir = Path.GetDirectoryName(target);
- if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); }
- MessageBox.Show("Start " + source.AbsoluteUri);
- Downloading = true;
- _target = target;
- FileNumber++;
- CurrentBytes = 0;
- _request = (HttpWebRequest)HttpWebRequest.Create(source);
- _request.UserAgent = _userAgent;
- _request.BeginGetResponse(new AsyncCallback(ResponseReceived), Instance);
- }
- private void ResponseReceived(IAsyncResult async)
- {
- try
- {
- _response = (HttpWebResponse)_request.EndGetResponse(async);
- } catch (Exception ex)
- {
- MessageBox.Show(ex.Message); // testcode
- throw ex; // TODO: Find out which exception throws here - literaly a WebException - catch and do correctt sttuff like rettry in x seconds or stuff
- }
- CurrentDownloadLength = (int)_response.ContentLength;
- if (BeginDownload != null) BeginDownload.Invoke(_target);
- Array.Resize(ref _dataBuffer, CurrentDownloadLength);
- _fileStream = new FileStream(_target, FileMode.Create);
- _response.GetResponseStream().BeginRead(_dataBuffer, 0, CurrentDownloadLength, new AsyncCallback(OnDataRead), Instance);
- }
- private void OnDataRead(IAsyncResult async)
- {
- int nBytes = _response.GetResponseStream().EndRead(async);
- _fileStream.Write(_dataBuffer, 0, nBytes);
- if (nBytes > 0)
- {
- CurrentBytes += nBytes;
- if (UpdateDownload != null) UpdateDownload.Invoke();
- _response.GetResponseStream().BeginRead(_dataBuffer, 0, CurrentDownloadLength, new AsyncCallback(OnDataRead), Instance);
- } else
- {
- MessageBox.Show("END");
- _fileStream.Close();
- _fileStream.Dispose();
- if (EndDownload != null) EndDownload.Invoke();
- Downloading = false;
- }
- }
- public delegate void DBeginDownload(string targetFile);
- public delegate void DUpdateDownload();
- public delegate void DEndDownload();
- }
- }
-
Irgendwer ne Idee oder Hilfestellung?