I'm new at YouTube Data API as well as C#. I want to search the video and display the result into a listview and I did but I can't did it asynchronously. I've se开发者_运维技巧arched and try a lot but end up with nothing.
http://google-gdata.googlecode.com/svn/docs/folder48/Multithreadedoperations.html
Please help me.
Most WebService API's (especially RESTful ones) do not provide an async way to retrieve results, since in general, they try to remain stateless: I believe that the Youtube API is no exception.
Instead you will need to lean on traditional methods for simulating async requests, such as one of the following:
- Make smaller, paged requests, and only fire them off as your users scroll through the results and you have none left to display. This means there is less work to do on each request and your UI will be less likely to lock.
- Even if you do this, you may want to use a dedicated thread to take care of fetching the results and feeding them back to the thread responsible for updating your UI. This will ensure your UI remains responsive whatever the latency of the server.
- Alternatively, use an event based framework (a framework that implements the reactor pattern) to make the requests. Under such frameworks you fire off a request and get a callback with the results, allowing you to do other things in the meantime. Under this model you trade off the need for mutexes to protect shared data (used when threading) against the (arguably) added complexity of the callback model.
[edit: for .NET I see that might not be such a good candidate for the reactor based approach]
There is nothing special about YouTube in this regard: you might employ the same techniques when fetching large result sets from most API's.
Can't you wrap any non async method in a asynchronous call using asynchronous delegates? My reference is this link.
Ok So did you make your async call something like this ...
private delegate <returnType> YouTubeAPI( <args> )
private YouTubeAPI func;
private IAsyncResult ticket;
void YouTubeSearchFunc( string what )
{
func = <whatever the you tube call is>
ticket = func.BeginInvoke( <args needed> );
// WE MUST do this in order to return flow of control to program
return;
}
// later we need to check if the ticket is done and then get it
if ( ticket.IsComplete == true )
// get it
else
// continue on like normal
The reason we cant just immediately get the value is because that will block the program. This is exactly what we don't want to do. So instead, we have to structure the program in a way that we check to see if its done on a separate update. This lets Win-forms or GTK run so that it doesn't look like it hangs. UNFORTUNATELY, this is a pain in the butt. What you probably want to do is just use a background worker. Read this part HERE and see if this doesn't help you more because BackgroundWorkers can have callbacks. Then all you have to do is set up a background worker, and then hook your update function into the completed event.
精彩评论