0

I want to Pick a file for unit testing parts of the application. how ever this is causing deadlock.

If I put breakpoint at CoreApplication line (before Assert.IsNotNull) and start debugging by pressing F10 it wont dead lock, but I get dead lock without breakpoint.

If I mark method as async and await result, I get InvalidOperationException saying

A method was called at an unexpected time.

How should I fix this issue?

private StorageFile file;    

//[TestMethod, TestCategory("Basics")]
public void T01_PickFile()
{
    // initialize picker
    var picker = new FileOpenPicker
    {
        SuggestedStartLocation = PickerLocationId.Desktop,
        ViewMode = PickerViewMode.List
    };
    picker.FileTypeFilter.Add(".txt");

    // grant access and pick file
    // deadlock if there is no breakpoint
    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        file = picker.PickSingleFileAsync().GetAwaiter().GetResult();
    }).GetAwaiter().GetResult();

    Assert.IsNotNull(file);
}

Update:

if I wait for result asynchronously the application does not wait and continues the execution before I pick my file and Assert.IsNotNull(file) fails the test.

note: I see FileOpenPicker comes for a second then test fails.

//[TestMethod, TestCategory("Basics")]
public async Task T01_PickFile()
{
    // initialize picker
    var picker = new FileOpenPicker
    {
        SuggestedStartLocation = PickerLocationId.Desktop,
        ViewMode = PickerViewMode.List
    };
    picker.FileTypeFilter.Add(".mid");

    // grant access and pick file
    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
    {
        file = await picker.PickSingleFileAsync();
    });

    Assert.IsNotNull(file);
}

This is how I call this method

[TestMethod, TestCategory("Basics")]
public async Task T02_OpenTest()
{
    await T01_PickFile();
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 1
    You can't wait on a result in the UI thread. That's guaranteed deadlock. You have to use `await`. See marked duplicate. In your code, you should skip `RunAsync()` altogether, and just `file = await picker.PickSingleFileAsync()` – Peter Duniho Apr 24 '17 at 05:36
  • see the update. sorry I should've write that too before posting. Ill check out the duplicate. – M.kazem Akhgary Apr 24 '17 at 05:36
  • `InvalidOperationException` means you've done something else wrong. But the solution is to stop doing that something else wrong, not to add to your problems by blocking the UI thread while you wait for the result. If you're unable to get `await` to work, you need to post a new question with a good [mcve] that reliably reproduces the problem, so someone can help you with _that_. – Peter Duniho Apr 24 '17 at 05:38
  • Thanks, please see the update. – M.kazem Akhgary Apr 24 '17 at 05:55
  • The issue was due to `Dispatcher.RunAsync` which did not wait for completion of its delegate. I found solution here http://stackoverflow.com/questions/19133660/runasync-how-do-i-await-the-completion-of-work-on-the-ui-thread @PeterDuniho – M.kazem Akhgary Apr 24 '17 at 06:09
  • You have now completely changed your question, which is a big no-no on Stack Overflow. Edits are fine, encouraged even, for clarity, fixing typos, etc. But you can't go changing the fundamental nature of the question. Going from "code deadlocks" to "doesn't wait" is completely different. That said, your problem in the second question appears to be the use of `RunAsync()`. Like I said above, don't use that. – Peter Duniho Apr 24 '17 at 06:09
  • yes, you are right, the question is completely changed in Update. I had to be more patient than asking this on SO. btw use of dispatcher is required, other wise `PickSingleFileAsync` fails with error `Invalid Window Handle` – M.kazem Akhgary Apr 24 '17 at 06:18

0 Answers0