Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > Простой системы при закачке файла


Автор: stashe 6.5.2008, 14:38
Приложение закачивает определенный файл с сервера путем
 
Client.DownloadFile("http://www.../app/file.ex_", Application.StartupPath + "\\Update\\file.exe");     

кто подскажет как организовать прогресс бар чтобы во время закачки система не простаивала. В другом потоке?. Написать то можно, хотелось бы 'красивый и компактный" пример. Спасибо за уделенное время

Автор: mr.DUDA 6.5.2008, 17:40
Не оскудевает копилка примеров MSDN:

The following code example demonstrates setting an event handler for this event. (речь идёт о WebClient..::.DownloadProgressChanged)
Код
// Sample call : DownLoadFileInBackground2 ("http://www.contoso.com/logs/January.txt");
public static void DownLoadFileInBackground2 (string address)
{
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadFileCallback method gets called
    // when the download completes.
    client.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback2);
    // Specify a progress notification handler.
    client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
    client.DownloadFileAsync (uri, "serverdata.txt");
}


The following code example shows an implementation of a handler for this event:
Код
private static void UploadProgressCallback(object sender, UploadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    uploaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesSent, 
        e.TotalBytesToSend,
        e.ProgressPercentage);
}

private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
    // Displays the operation identifier, and the transfer progress.
    Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...", 
        (string)e.UserState, 
        e.BytesReceived, 
        e.TotalBytesToReceive,
        e.ProgressPercentage);
}

Автор: stashe 7.5.2008, 10:56
А если с помощью Client.DownloadFile, а не асинхронно?

Автор: mr.DUDA 7.5.2008, 16:31
Тогда не выйдет. Можно только через Client.DownloadFileAsync (см. пример выше).

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)