This project has moved. For the latest updates, please go here.

How to detect if Callback has been made?

Aug 21, 2012 at 9:52 PM

Hi,

first thank you for the library, I have a suggestion, could you add the override tostring() on classes such as TmdbMovie, etc that way the object can be added to forms controls such as listbox, combobox?

I just started using the asysnc calls but can't figure out how to determine if the callback has been made.

Regards

Bill

 

Coordinator
Aug 24, 2012 at 7:32 AM
Hi Bill,

Have now updated a lot of the class to override the ToString, and will essentially return the "name" or "title" member of that class...is that what you were after? Will commit the changes over the weekend.

Regarding async calls, one suggestion would be to use a flag to know when the callback has been made, by setting that flag in the callback.

bool callbackReceived = false;
api.SearchMovie("Aliens", 1, callbackReceived, result=>
{
// this is the callback
bool received = (bool)result.UserState;
received = true;
});

while (callbackReceived == false)
waitforperiod;

Obviously you wouldn't want to do this in production, as it defeats the purpose of using the async methods...the while/wait will block the processing thread until the callback is received.

I guess it depends on what you're doing. Using Windows Phone, for example, you could set the results into the ItemSource property of a listbox...all within the callback.

Let me know if you have more specifics and I'll try and help out.

Cheers,
Craig

On Wed, Aug 22, 2012 at 7:52 AM, wbroek <notifications@codeplex.com> wrote:

From: wbroek

Hi,

first thank you for the library, I have a suggestion, could you add the override tostring() on classes such as TmdbMovie, etc that way the object can be added to forms controls such as listbox, combobox?

I just started using the asysnc calls but can't figure out how to determine if the callback has been made.

Regards

Bill

Read the full discussion online.

To add a post to this discussion, reply to this email (wattmdb@discussions.codeplex.com)

To start a new discussion for this project, email wattmdb@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Aug 24, 2012 at 9:16 AM
Hi Craig

Thanks for reply, yes regarding the override ToString that's what I did to my copy of the library, I think it is a good update!

Regarding the callback issue, I ended up using an enumerated Flag, 1 bit for each type of async call I used that way I can test for any outstanding callbacks. I was wondering if the library could have a property to indicate a callback is pending

Regards,

Bill

On 24/08/2012, at 5:33 PM, "watway"<notifications@codeplex.com> wrote:

From: watway

Hi Bill,

Have now updated a lot of the class to override the ToString, and will essentially return the "name" or "title" member of that class...is that what you were after? Will commit the changes over the weekend.

Regarding async calls, one suggestion would be to use a flag to know when the callback has been made, by setting that flag in the callback.

bool callbackReceived = false;
api.SearchMovie("Aliens", 1, callbackReceived, result=>
{
// this is the callback
bool received = (bool)result.UserState;
received = true;
});

while (callbackReceived == false)
waitforperiod;

Obviously you wouldn't want to do this in production, as it defeats the purpose of using the async methods...the while/wait will block the processing thread until the callback is received.

I guess it depends on what you're doing. Using Windows Phone, for example, you could set the results into the ItemSource property of a listbox...all within the callback.

Let me know if you have more specifics and I'll try and help out.

Cheers,
Craig

On Wed, Aug 22, 2012 at 7:52 AM, wbroek <notifications@codeplex.com> wrote:

From: wbroek

Hi,

first thank you for the library, I have a suggestion, could you add the override tostring() on classes such as TmdbMovie, etc that way the object can be added to forms controls such as listbox, combobox?

I just started using the asysnc calls but can't figure out how to determine if the callback has been made.

Regards

Bill

Read the full discussion online.

To add a post to this discussion, reply to this email (wattmdb@discussions.codeplex.com)

To start a new discussion for this project, email wattmdb@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Aug 24, 2012 at 9:42 AM

Hi Bill,

Have just added an AsyncCount member to Tmdb class that will indicate the number of callbacks still outstanding.  From what I can determine, the increment/decrement operation on an 'int' is atomic and threadsafe so this *should* work.  Have tested by throwing a large number of async calls at it at once and the AsyncCount worked perfectly.  Hope this will do what you're after.

Cheers,

Craig

Aug 25, 2012 at 1:45 AM

Hi Craig

Thanks for the alterations much appreciated, may have detected a bug I was testing your way of passing a flag through the userstate object, when you pass in a Bool or an Int you pass in a copy not a reference to the object, hence the original does not get updated in the callback.

Cheers,

bill

From: watway [email removed]
Sent: Friday, 24 August 2012 7:43 PM
To: wbroek@bigpond.com
Subject: Re: How to detect if Callback has been made? [wattmdb:392526]

From: watway

Hi Bill,

Have just added an AsyncCount member to Tmdb class that will indicate the number of callbacks still outstanding. From what I can determine, the increment/decrement operation on an 'int' is atomic and threadsafe so this *should* work. Have tested by throwing a large number of async calls at it at once and the AsyncCount worked perfectly. Hope this will do what you're after.

Cheers,

Craig

Coordinator
Aug 25, 2012 at 1:57 AM

Hi Bill,

Ah yes, you're correct...I admit I didn't test that before I wrote it.  I had considered whether or not to use the bool as the UserData...given the example is while/wait in the scope that defined the bool, it wasn't necessary...just set callbackReceived = true in the callback.

            bool callbackReceived = false;
            api.SearchMovie("Aliens", 1, null, result =>
                {
                    callbackReceived = true;
                });
            while (callbackReceived == false)
            {
                // Still waiting
            }
            // callback has been received

Even tested it this time :)

Cheers,

Craig

Aug 25, 2012 at 6:03 AM
Edited Aug 25, 2012 at 7:44 AM

Hi Craig

 Just to let you know this is what I implemented

 

[Flags]
public enum AsyncCallBackStatus : long
{
    // true value indicates waiting for callback from api call
    None = 0x0,
    MovieSearch = 0x1,
    MovieInfo = 0x2,
    MovieGenre = 0x4,
    MovieCast = 0x8,
    MovieImages = 0x10,
    MovieTrailers = 0x20,
    MovieImdb = 0x40,
}

public class AsyncStatus
{
     private AsyncCallBackStatus _status = AsyncCallBackStatus.None;
     private object _asyncLocker = new object();
     public void SetStatusBit(AsyncCallBackStatus Bit)
     {
         lock (_asyncLocker)
         {
             _status |= Bit;
         }
     }

     public void ResetStatusBit(AsyncCallBackStatus Bit)
     {
         lock (_asyncLocker)
         {
             _status &= ~Bit;
         }
     }

     public bool TestStatusBit(AsyncCallBackStatus Bit)
     {
         lock (_asyncLocker)
         {
             return ((_status & Bit) == Bit);
         }
     }

     public AsyncCallBackStatus Status
     {
         get
         {
             lock (_asyncLocker)
             {
                 return _status;
             }
         }
    }
}

And the main code has this

private AsyncStatus _asyncStatus = new AsyncStatus();
_asyncStatus.SetStatusBit(AsyncCallBackStatus.MovieCast);
_api.GetMovieCast(id, _asyncStatus, result =>
    {
        ((AsyncStatus)result.UserState).ResetStatusBit(AsyncCallBackStatus.MovieCast);
    });

 Someone may find this usefull

  Regards

Bill

 

From: watway [email removed]
Sent: Saturday, 25 August 2012 11:57 AM
To: wbroek@bigpond.com
Subject: Re: How to detect if Callback has been made? [wattmdb:392526]

 

From: watway

Hi Bill,

Ah yes, you're correct...I admit I didn't test that before I wrote it. I had considered whether or not to use the bool as the UserData...given the example is while/wait in the scope that defined the bool, it wasn't necessary...just set callbackReceived = true in the callback.

            bool callbackReceived = false;
            api.SearchMovie("Aliens", 1, null, result =>
                {
                    callbackReceived = true;
                });
            while (callbackReceived == false)
            {
                // Still waiting
            }
            // callback has been received

Even tested it this time :)

Cheers,

Craig