:App kapatırsa kullanımı 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);
ve bu dosyayı indirmek için yapılması gerekenler: Ben kullanıcıya Kalan zaman göstermek istiyorum ben indirme düğmesine bastığınızda
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;
});
}
, uygulaması şu anda askıda gider ve indirme uçlarına kadar bunu kullanamaz ve bu kod üzerinde çalışır Görsel stüdyondaki çıkış pencereleri ancak UI askıya alındı ve textBl'de sonuç gösterilemiyor ock.
Bu sorunu nasıl düzeltebilirim? Teşekkürler
Sadece 'button' düğmesini tıklamanın yanı sıra bazı sınır değerleri ayarlayıp koddan çıkmaya ne dersiniz ..? kodda nerede uygulamanızın kapanmaya başladığını söyler misiniz ..? – MethodMan