E
E
Eugene2016-12-15 13:27:23
WPF
Eugene, 2016-12-15 13:27:23

Why is this code blocking the UI?

Why is this code blocking the UI when it should run asynchronously?

private async void MenuOpen_Click(object sender, RoutedEventArgs e) {
            Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog();
            fileDialog.DefaultExt = ".txt";
            fileDialog.Filter = "Text file|*.txt|All files|*.*";
            fileDialog.Multiselect = false;

            if (!(bool)fileDialog.ShowDialog())
                return;

            string content = String.Empty;

            using (var file = new StreamReader(fileDialog.FileName)) {
                content = await file.ReadToEndAsync();
            }

            tbLeft.Text = content;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
none7, 2016-12-15
@Jek_Rock

And it's definitely not the line tbLeft.Text = content; ? Ordinary TextBoxes did not digest large amounts of text well. And this line will definitely block the UI.

M
Mercury13, 2016-12-15
@Mercury13

Because OpenFileDialog is modal.
Added the async keyword and expect a different behavior from the standard Windows dialog?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question