Thread stoppt wenn keine UI-Action oder nicht Debug

  • C#
  • .NET (FX) 4.0

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Thread stoppt wenn keine UI-Action oder nicht Debug

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

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.IO;
    7. using System.Net;
    8. using System.Windows.Forms;
    9. namespace kcUpdater.Classes
    10. {
    11. public class WebDownloader
    12. {
    13. private static readonly Lazy<WebDownloader> lazy = new Lazy<WebDownloader>(() => new WebDownloader());
    14. /// <summary>
    15. /// default useragend for web requests
    16. /// </summary>
    17. private static string _userAgent = "kcUpdate/0.3 (compatible; .NET4.0C; .NET4.0E; .NET4.5C; .NET4.5E; .NET4.6C; .NET4.6E)";
    18. /// <summary>
    19. /// Returns the Instance of this class
    20. /// </summary>
    21. public static WebDownloader Instance { get { return lazy.Value; } }
    22. public bool Downloading;
    23. public int FileNumber = 0;
    24. public int CurrentBytes;
    25. public int CurrentDownloadLength;
    26. public DBeginDownload BeginDownload;
    27. public DUpdateDownload UpdateDownload;
    28. public DEndDownload EndDownload;
    29. private string _target;
    30. private HttpWebRequest _request;
    31. private HttpWebResponse _response;
    32. private byte[] _dataBuffer;
    33. private FileStream _fileStream;
    34. /// <summary>
    35. /// Make the constructor private!
    36. /// </summary>
    37. private WebDownloader()
    38. {
    39. Downloading = false;
    40. }
    41. public void DownloadFile(Uri source, string target)
    42. {
    43. MessageBox.Show("Initiate " + source.AbsoluteUri);
    44. while (Downloading) { } //NOP - wait for current download to finnish
    45. MessageBox.Show("Ensure " + source.AbsoluteUri);
    46. string dir = Path.GetDirectoryName(target);
    47. if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); }
    48. MessageBox.Show("Start " + source.AbsoluteUri);
    49. Downloading = true;
    50. _target = target;
    51. FileNumber++;
    52. CurrentBytes = 0;
    53. _request = (HttpWebRequest)HttpWebRequest.Create(source);
    54. _request.UserAgent = _userAgent;
    55. _request.BeginGetResponse(new AsyncCallback(ResponseReceived), Instance);
    56. }
    57. private void ResponseReceived(IAsyncResult async)
    58. {
    59. try
    60. {
    61. _response = (HttpWebResponse)_request.EndGetResponse(async);
    62. } catch (Exception ex)
    63. {
    64. MessageBox.Show(ex.Message); // testcode
    65. throw ex; // TODO: Find out which exception throws here - literaly a WebException - catch and do correctt sttuff like rettry in x seconds or stuff
    66. }
    67. CurrentDownloadLength = (int)_response.ContentLength;
    68. if (BeginDownload != null) BeginDownload.Invoke(_target);
    69. Array.Resize(ref _dataBuffer, CurrentDownloadLength);
    70. _fileStream = new FileStream(_target, FileMode.Create);
    71. _response.GetResponseStream().BeginRead(_dataBuffer, 0, CurrentDownloadLength, new AsyncCallback(OnDataRead), Instance);
    72. }
    73. private void OnDataRead(IAsyncResult async)
    74. {
    75. int nBytes = _response.GetResponseStream().EndRead(async);
    76. _fileStream.Write(_dataBuffer, 0, nBytes);
    77. if (nBytes > 0)
    78. {
    79. CurrentBytes += nBytes;
    80. if (UpdateDownload != null) UpdateDownload.Invoke();
    81. _response.GetResponseStream().BeginRead(_dataBuffer, 0, CurrentDownloadLength, new AsyncCallback(OnDataRead), Instance);
    82. } else
    83. {
    84. MessageBox.Show("END");
    85. _fileStream.Close();
    86. _fileStream.Dispose();
    87. if (EndDownload != null) EndDownload.Invoke();
    88. Downloading = false;
    89. }
    90. }
    91. public delegate void DBeginDownload(string targetFile);
    92. public delegate void DUpdateDownload();
    93. public delegate void DEndDownload();
    94. }
    95. }


    Irgendwer ne Idee oder Hilfestellung?

    Kagu-chan schrieb:

    und fängt niemals mit der zweiten an.
    Auch wenn d as jetzt nicht 100 prozentig passt: Gugst Du hier.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!