Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't it update the ui then run the code wpf

I have this code where I upload some streams to an ftp server. But before that I want to make the spinnerContainer visible. But if I call the method it freezez (It started uploading) and only show the container after it’s finished.

spinnerContainer.Visibility = Visibility.Visible;

client.Connect();
client.EmptyDirectory(Id);
client.SetWorkingDirectory(Id);
for (int i = 0; i < streams.Count; i++)
{
    client.UploadStream(streams[i], i.ToString());
}
client.Disconnect();

I want to run the spinnerContainer.Visibility = Visibility.Visible; first then the rest. How can I achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code runs on the UI thread, so until the UI method completes execution, the UI thread will be blocked and the GUI will not update.
Use asynchrony:

private async void SomeMethod()
{
    spinnerContainer.Visibility = Visibility.Visible;

    await Task.Run(() =>
    {
        client.Connect();
        client.EmptyDirectory(Id);
        client.SetWorkingDirectory(Id);
        for (int i = 0; i < streams.Count; i++)
        {
            client.UploadStream(streams[i], i.ToString());
        }

        client.Disconnect();
    });

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading