locked
Problem opening a stream over a file in a C# WinRT component

    Question

  • I'm developing a C# WinRT component in a small test app. I want to get a StorageFile (which works), open a stream over the file (which sometimes works), and read in bytes.

    When I open a stream in the main class of the test app, everything works just as expected.

    When I use the exact same OpenStreamForReadAsync method in the component, it doesn't work. It doesn't throw an exception; it simply stops executing and goes back to the main class. I can't tell exactly what's going on, since no exception is thrown. There's no indication that anything has gone wrong, except that I never hit breakpoints after the stream line.

    I'll try stepping through, and the component will get the StorageFile, but when I advance to the line where it opens the stream, nothing. It just continues from the line in the main class of the app immediately after the call to the method in the component.

    This is the code that I use in both places.

    StorageFolder dir = ApplicationData.Current.TemporaryFolder;
    string filename = "testfile.txt";
    StorageFile file = await dir.GetFileAsync(filename);
    
    Stream stream = await file.OpenStreamForReadAsync();
    

    What I've tried / what I'm doing:

    • Making sure the file exists, and I have read/write permission. It does, and I do.
    • Using the other StorageFile.OpenWhateverAsync() methods. None of them worked, either.
    • Explicitly threw an exception to make sure my try/catch block is working. It is.
    • Reading all of the documentation, blog posts, and fora that I could find.

    Is there something about file access in WinRT components or asynchronous I/O in general that I'm missing? This looks like a problem with doing things asynchronously, but the thing where it seems to stop executing is new to me.

    Parenthetically, this is for an undergrad senior project that's being presented in a few weeks. So I'm both relatively inexperienced and pressed for time.

    Tuesday, April 15, 2014 8:33 PM

Answers

  • I think the problem here is that you're using an async void - not a good thing to do.

    I'd read "Best Practices in Asynchronous programming" and modify the StreamTest function to be declared like this:

    public static async Task StreamTest(string filename)
    {
        /// do work here.
    }


    Darin R.

    Thursday, April 17, 2014 4:32 PM

All replies

  • Threading sounds likely. What thread are you calling this on? Do you get better results of you dispatch to the UI thread?

    For more details, can you provide a minimal sample that demonstrates the problem?

    Tuesday, April 15, 2014 10:28 PM
    Owner
  • Sample:

    In App.xaml.cs

    protected override void OnLaunched(LaunchActivatedEventArgs e) {
        // ...
    
        myFunction();
    }
    
    private void myFunction() {
        TestComponent.StreamTest("testfile.txt");
    }
    

    In TestComponent.cs, the sole class in a WinRT component:

    public static async void StreamTest(string filename) { StorageFolder dir = ApplicationData.Current.TemporaryFolder; StorageFile test = await dir.GetFileAsync(filename); FileRandomAccessStream stream = (FileRandomAccessStream)(await test.OpenAsync(FileAccessMode.ReadWrite)); }

    That's really all there is to it. In App.xaml.cs, I call a method in the component that tries to open a stream. The FileRandomAccessStream stream = ... line is where the problem happens.

    I don't know about dispatching to the UI thread. I just did some reading, but I don't quite understand how to do it.

    Wednesday, April 16, 2014 2:16 PM
  • I think the problem here is that you're using an async void - not a good thing to do.

    I'd read "Best Practices in Asynchronous programming" and modify the StreamTest function to be declared like this:

    public static async Task StreamTest(string filename)
    {
        /// do work here.
    }


    Darin R.

    Thursday, April 17, 2014 4:32 PM
  • That was it. I changed the return type and I can open streams and files. Thank you!

    Tuesday, April 22, 2014 3:13 AM