RKh
RKh

Reputation: 14161

WPF Window does not finish rendering when download starts

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 Window_Loaded event.

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

Upvotes: 0

Views: 245

Answers (3)

Bathineni
Bathineni

Reputation: 3496

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

Upvotes: 0

Eddie Paz
Eddie Paz

Reputation: 2241

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

Upvotes: 0

Jens
Jens

Reputation: 25563

Have a look at the DownloadFileAsync method.

Upvotes: 1

Related Questions