i C# bin mit einer App zu entwickeln, die eine Datei aus dem Internet herunterladen (Ich will nicht Hintergrund-Downloader verwenden!) Hier Klassencode zum Download:App hängt beim Gebrauch HttpWebRequest
public class DL
{
public event Progresses OnProgress;
Stopwatch stopwatch = new Stopwatch();
public async void Get(string url, StorageFile destinationFile)
{
stopwatch.Reset();
stopwatch.Start();
HttpWebRequest request = (HttpWebRequest)WebRequest.
Create(url);
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
long size = response.ContentLength;
long downloaded = 0;
using (Stream inputStream = response.GetResponseStream())
using (Stream outputStream = await destinationFile.OpenStreamForWriteAsync())
{
byte[] buffer = new byte[1024];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
downloaded += bytesRead;
outputStream.Write(buffer, 0, bytesRead);
int secondsRemaining = (int)(stopwatch.Elapsed.TotalSeconds
/downloaded * (size - downloaded));
TimeSpan span = new TimeSpan(0, 0, 0, secondsRemaining);
string remainTime = string.Format("{0}:{1}:{2}", span.Hours, span.Minutes, span.Seconds);
OnProgress(remainTime);
} while (bytesRead != 0);
}
}
}
public delegate void Progresses(string text);
und dies ist, wie eine Datei zum Download:
private async void btnDownload_Click(object sender, RoutedEventArgs e)
{
DL webGard = new DL();
webGard.OnProgress += WebGard_OnProgress;
StorageFile destinationFile = await KnownFolders.MusicLibrary
.CreateFileAsync("r58.zip", CreationCollisionOption.GenerateUniqueName);
string url = "my url";
webGard.Get(url, destinationFile);
}
private async void WebGard_OnProgress(string text)
{
System.Diagnostics.Debug.WriteLine(text);
var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
await dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() =>
{
textBlock.Text = text;
});
}
Wenn ich Download-Button drücken, App geht zur Zeit zu hängen und kann es nicht bis Download endet verwenden, möchte ich verbleibende Zeit Benutzer zeigen, und dieser Code funktioniert auf Ausgabefenster in Visual Studio, aber UI ist gehängt und kann keine Ergebnisse in textBl anzeigen ock.
Wie kann ich dieses Problem beheben? Danke
Wie wäre es, wenn Sie nicht nur auf die Schaltfläche klicken, sondern einige Haltepunkte setzen und den Code durchgehen? kannst du uns sagen, wo im Code deine App anfängt aufzuhängen ..? – MethodMan