Domanda

VS 2008 SP1

Sto usando DownloadStringAysnc. Ma l'evento ProgressChanged non mostra i progressi fino a quando la stringa non è stata scaricata.

Anche quando provo a scaricare una stringa contenuta in un file di grandi dimensioni. I programmi rimangono di risposta, quindi so che sta facendo qualcosa. Tuttavia, è quando l'avanzamento è stato completato che l'evento progressChanged si attiva.

L'ho conosciuto come il progressChanged e il DownloadStringCompleted immediatamente dopo l'altro. Tuttavia, dovrebbero essere una pausa in quanto il file è piuttosto grande.

Questo è lo snippet di codice che sto attualmente utilizzando. E l'output di seguito. Ciò che è strano e.progresspercentage è del 100%. E sembra essere chiamato due volte.

Mille grazie per qualsiasi consiglio

Output in the progress changed event
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed Version userstate: [ Version1 ]


private void UpdateAvailable()
        {
            WebClient wbCheckUpdates = new WebClient();
            wbCheckUpdates.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wbCheckUpdates_DownloadProgressChanged);
            wbCheckUpdates.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wbCheckUpdates_DownloadStringCompleted);
            DownloadFiles df = new DownloadFiles();
            string webServerURL = df.webServerPath;

            wbCheckUpdates.DownloadStringAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1"); 
        }




void wbCheckUpdates_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Console.WriteLine("Progress version changed userstate: [ " + e.UserState + " ]");
            progressBar1.Value = e.ProgressPercentage;
            Console.WriteLine("progressBar1.Value [ " + this.progressBar1.Value + " ]");
        }

void wbCheckUpdates_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            Console.WriteLine("Completed version userstate: [ " + e.UserState + " ]");
        }

=========== Modificato utilizzando DownloadDataAysnc ===============

wbCheckUpdates.DownloadDataAsync(new Uri(Path.Combine(webServerURL, "version.txt")), "Version1");


void wbCheckUpdates_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            byte[] result = e.Result;           
            Console.WriteLine("Completed data: [ " + System.Text.ASCIIEncoding.Default.GetString(result) + " ]");
        }

I risultati sono gli stessi:

Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Progress changed Version userstate: [ Version1 ]
progressBar1.Value [ 100 ]
Completed data: [ 1.0.11 ]
È stato utile?

Soluzione

Secondo la documentazione, DownloadStringAsync non segnala i progressi. Consulta la documentazione del WebClient.DownloadProgressChanged Event .

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top