Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7753567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:05:16+00:00 2026-06-01T12:05:16+00:00

I have a simple metro app contains a button, a label and a drop

  • 0

I have a simple metro app contains a button, a label and a drop down list. The drop down list contains a list of files that I can read from. When I click on the button, the selected file’s content gets read into the label. Files are in the Documents folder (i.e WinRT’s KnownFolders.DocumentsLibrary). Each file represents a StorageFile in WinRT API.

File reading method is an asynchronous method (uses async/await). In order to prove the asynchronous behaviour, I made the file reading method as a long running process. Therefore, while executing this long running method, I should be able to freely click on the drop down list and select a different file. This is because the UI thread should not get blocked while reading the file. However this is not currently happening. It still seems to be blocking UI thread, and drop down list get frozen while the long running method occurs.
I must be doing something odd here. Can you please tell me why the UI is non responsive?
My code sample is below.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace FileAccess
{
    public sealed partial class MainPage : Page
    {
       private readonly StorageFolder storageFolder;       

       public MainPage()
       {
        this.InitializeComponent();
        storageFolder = KnownFolders.DocumentsLibrary;
        AddFiles();
       }

       private async void AddFiles() //Add files to the drop down list
       {
        var filesList = await storageFolder.GetFilesAsync();
        IEnumerable<FileItem> fileItems
            = filesList.Select(x => new FileItem(x.Name, x.Name));

        cboSelectFile.ItemsSource = fileItems;
        cboSelectFile.SelectedIndex = 0;
       }


      private async void BtnReadFile_Click(object sender, RoutedEventArgs e)
      {
        IStorageFile storageFile = await storageFolder.GetFileAsync((string)cboSelectFile.SelectedValue);           

        if (storageFile != null)
        {
            LblReadFile.Text = await ReadFileAsync(storageFile); //long running method**************
        }
      }


      private async Task<string> ReadFileAsync(IStorageFile storageFile) //long running method**************
      {
        var fileContent = await FileIO.ReadTextAsync(storageFile);

        for (Int64 i = 0; i < 10000000000; i++)
        {
        }

        return fileContent; 
      }                
  }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:05:17+00:00Added an answer on June 1, 2026 at 12:05 pm

    If you execute code like this on a UI thread:

    var whatever = await DoSomethingAsync();
    // some more code
    

    Then // some more code will also execute on the UI thread. Which is exactly your problem. After the file is read, you execute the long loop on the UI thread, which is why the UI freezes.

    If you want to simulate some long-running operation, you can do it several ways:

    1. Execute the loop on a background thread using Task.Run(), and asynchronously wait for it to finish:

      private async Task<string> ReadFileAsync(IStorageFile storageFile)
      {
          var fileContent = await FileIO.ReadTextAsync(storageFile);
      
          await Task.Run(() => { for (Int64 i = 0; i < 10000000000; i++) {} });
      
          return fileContent; 
       }
      
    2. Don’t waste CPU time and use Task.Delay() to delay executing of the code:

      private async Task<string> ReadFileAsync(IStorageFile storageFile)
      {
          var fileContent = await FileIO.ReadTextAsync(storageFile)
                                        .ConfigureAwait(false);
      
          await Task.Delay(TimeSpan.FromSeconds(10));
      
          return fileContent; 
       }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Netbeans 6.8 and metro 2.0 I have written a simple application that makes
I have simple serial port program that is supposed to read the serial port
I have simple one column HTML files ( ebooks from Gutenberg Project). I want
I have simple win service, that executes few tasks periodically. How should I pass
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
i have simple QStandardItemModel that holds the date to show in QTreeview when i
I have simple aspx page that has a form and a input field in
I have created a simple, light weight, GUI similar to Microsoft Metro UI .
I'll keep this one as simple as I can. I have a method in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.