开发者

WPF Window does not finish rendering when download starts

开发者 https://www.devze.com 2023-03-22 23:20 出处:网络
I am writing a small file download utility. The DownloadFile() method is called on Window_Loaded() event of my MainWindow. Since the DownloadFile method is resource intensive, the MainWindow does not

I am writing a small file download utility. The DownloadFile() method is called on Window_Loaded() event of my MainWindow. Since the DownloadFile method is resource intensive, the MainWindow does not finish rendering on screen when the download starts. It is just after the download is finished that I come to see the actual controls on my WPF Form.

To control this, I am using following DoEvents() code, but still it is not working. I am calling this function after InitializeComponent() in the Form constructor and just before calling DownloadFile() in the Wind开发者_如何学编程ow_Loaded event.

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


Have a look at the DownloadFileAsync method.


it is always suggested to do all the server hits and download related things in a separate thread. You can see how to that in following link.

http://bathinenivenkatesh.blogspot.com/2011/07/wpf-build-more-responsive-ui.html


You should use BeginInvoke instead (with the Background option) and put the DownloadFile there.

0

精彩评论

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

关注公众号