0

I passed "GetFilesRevisions_Results" method for GetAsync which will handle the result for it.But I want to return a integer from "GetFilesRevisions_Results" method.How I can achieve this ? Thanks in advance!

private int GetVersionNumber(string i_sFileName)
{
    #region Get latest version no.
    int nVerNo = 0;
   // RequestResult result;
    try
    {

    OAuthUtility.GetAsync
       (
       "https://api.dropboxapi.com/1/revisions/auto/",
           new HttpParameterCollection
                        {
                           { "path", i_sFileName },
                           { "access_token", accessToken },
                           { "rev_limit", 1 }
                        },
          callback:  GetFilesRevisions_Results ??? How I can access return variable 
       );
    }
    catch
    { 

    }
    return nVerNo;
    #endregion
}

private int GetFilesRevisions_Results(RequestResult result)
{
    int nVerNo = 0;

    if (result.StatusCode == 200)
    {
        dynamic dynJson = JsonConvert.DeserializeObject(Convert.ToString(result));
        foreach (var item in dynJson)
        {
            nVerNo = Convert.ToInt32(item.rev);
        }

    }
    else
    {
        throw new Exception("Failed to get revisions of files");
    }

    return  nVerNo;
}
#endregion Get version Number
Deepak Pote
  • 125
  • 2
  • 6

2 Answers2

2

There's no way for you to know when your callback will be invoked, thus a return value is not the proper way to get your int.

You may use an event with an int argument and invoke it from within GetFilesRevisions_Results, just before the return. You will then be able to use your integer value from any of this event listeners.

aegar
  • 764
  • 9
  • 17
1

You probably want to use a wait handle:

AutoResetEvent waitHandle = new AutoResetEvent(false);
int nVerNoGlobalTempHolder = 0;

private int GetVersionNumber(string i_sFileName)
{
    #region Get latest version no.
    for (int i = 0; i < 10 && nVerNoGlobalTempHolder != 0; i++)
    {
       //Someone is waiting for this callback already...
       //Do something like:
       Thread.Sleep(500);
    }
    If (nVerNoGlobalTempHolder == 0) throw new Exception("timeout");

   // RequestResult result;
    try
    {

    OAuthUtility.GetAsync
       (
       "https://api.dropboxapi.com/1/revisions/auto/",
           new HttpParameterCollection
                        {
                           { "path", i_sFileName },
                           { "access_token", accessToken },
                           { "rev_limit", 1 }
                        },
          callback:  GetFilesRevisions_Results ??? How I can access return variable 
       );
    }
    catch
    { 

    }
    waitHandle.WaitOne();
    int nVerNo =nVerNoGlobalTempHolder;

    nVerNoGlobalTempHolder = 0;//Reset this in case you have multiple thread calling it
    return nVerNo;    
}


private int GetFilesRevisions_Results(RequestResult result)
{

    if (result.StatusCode == 200)
    {
        dynamic dynJson = JsonConvert.DeserializeObject(Convert.ToString(result));
        foreach (var item in dynJson)
        {
            nVerNoGlobalTempHolder = Convert.ToInt32(item.rev);
        }

    }
    else
    {
        throw new Exception("Failed to get revisions of files");
    }
    WaitHandle.Set();

}

This also implements very basic syncing in case more than one thread calls it. If you you dont need that, remove the the for loop at the start

srandppl
  • 571
  • 4
  • 14
  • 1
    If what you need is to handle the value from within `GetVersionNumber`, you will have to use a global scoped synchronization object. You may use a [captured variable](http://stackoverflow.com/questions/148669/local-variables-with-delegates) instead, by making your `GetFilesRevisions_Results` a delegate, but that would still be similar to a global variable. – aegar Dec 22 '15 at 08:44