开发者

Binding controls on WPF Form with a Notifer Class

开发者 https://www.devze.com 2023-03-23 17:51 出处:网络
I have a WPF Form with a Label and a Progress Bar on it. As soon as the Form is loaded, Download starts from a remote server but the Form freezes and is not rendered properly. In my last post, someone

I have a WPF Form with a Label and a Progress Bar on it. As soon as the Form is loaded, Download starts from a remote server but the Form freezes and is not rendered properly. In my last post, someone suggested me to use FileAsync to download file, but I am using a third-party FTP library. This libra开发者_运维技巧ry does have DownloadFileAsync method, but still the Form freezing problem persists.

I also created a DoEvents function like this, but it is also not stopping the interface from freezing.

private void DoEvents()
{
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
                                                  new Action(delegate { }));
}

I am planning another approach by using INotify type of thing. Let me tell you the steps I plan to do:

(1) Move the Download File method, to another Class, say DownloaderClass.

(2) When the MainWindow loads, create a separate thread and call the Download method in the Downloader Class.

(3) Create another Class, say, NotifierClass, as a Static Class. This class contains following properties:

DownloadedBytes,

CurrentFileName,

(4) The DownloadFile method in the DownloaderClass, keeps updating the properties of NotifierClass.

I want to connect the ProgressBar and few Label Controls on the MainWindow with these properties in Notifer Class to keep them updating.

Is this approach going to work? How to make the NotifierClass send status updates to the MainWindow?

I am using VS-2010 Express.


Skip the Notifier class and put the properties on the Download class. Make sure the download class implements INotifyPropertyChanged.

In your download method you can execute your code using a BackgroundWorker. If you use the Backgroundworkers ReportProgress method and event calls you can update your % complete property as you go and even execute code on the UI thread when complete.

Then you simply bind your downloadclass to the WPF form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号