locked
Strange problem with converter

    Question

  • Hello,

    I am facing very strange problem with converter. What I need to do is to check whether give file exist or not, but every time it returns random results, sometimes it behaves like file exits and sometimes it throws exception for filenotfound.

    May be this problem with async and await but don't know how to implement it inside converter.

    Here is the code.

    StorageFolder folder = ApplicationData.Current.TemporaryFolder;
    try
    {
      /* Also tried this
      * folder.GetFileAsync(filename); */                  
      var file = folder.GetFileAsync(value.tostring()).GetResults();
      return Application.Current.Resources["BlueTheme"];
    }
    catch (Exception ex)
    {
      return new SolidColorBrush(Colors.Red);
    }

    Thanks in advance.


    • Edited by zee_patel Friday, July 25, 2014 6:35 AM
    Friday, July 25, 2014 6:33 AM

Answers

  • I am not sure if GetResullt will block. Maybe try this:

      var file = folder.GetFileAsync(value.tostring()).AsTask().Result;


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Friday, July 25, 2014 8:10 AM
    Friday, July 25, 2014 8:01 AM

All replies

  • I am not sure if GetResullt will block. Maybe try this:

      var file = folder.GetFileAsync(value.tostring()).AsTask().Result;


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by zee_patel Friday, July 25, 2014 8:10 AM
    Friday, July 25, 2014 8:01 AM
  • Hi Dave, It Works Perfectly!. Thanks a lot. Could you please explain what was going wrong?
    Friday, July 25, 2014 8:09 AM
  • well... not really :) But what I suspected that you call GetResults before the async task is really complete. when that happens GetResults isn't waiting till the async operation is done but giving weird errors.

    When using the approach I wrote the .Result property will block the current thread till the operation is completed.


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Friday, July 25, 2014 8:50 AM