Asynchronous File Download with Progress Bar

后端 未结 4 1612
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 03:18

I am attempting to have a progress bar\'s progress change as the WebClient download progress changes. This code still downloads the file yet when I call s

4条回答
  •  执念已碎
    2020-11-29 04:22

    You should call startDownload() from the UI thread. The whole idea of WebClient.DownloadFileAsync() is that it will spawn a worker thread for you automatically without blocking the calling thread. In startDownload(), you specified callbacks that modify controls which I assume were created by the UI thread. Thus if you call startDownload() from a background thread it will cause problems, because a thread can only modify UI elements it created.

    The way it is supposed to work is you call startDownload() from the UI thread, startDownload() as you defined it sets up event call backs that are handled by the UI thread. It then starts the download asynchronously and returns immediately. The UI thread will be notified when the progress changes and the code responsible for updating the progress bar control will execute on the UI thread, and there shouldn't be any problems.

提交回复
热议问题