MonoDroid Activity Independent Task Queue?

2019-08-16 07:11发布

问题:

I have a Monodroid application that does many webrequests. I am making requests from activities. Because of this I have the need for a queue to process my requests outside of my activities. This is to avoid multiple requests being made at the same time.

Because I am making my requests on another thread using ThreadPool.QueueUserWorkItem it seems like if a user opens an activity, starts a request then goes back opens another activity and starts another request before the first request has finished the use of webrequests lock up and are no longer functional.

What would the best approach be to queue request tasks with callbacks outside of the activity or does anyone know/have an example of something similar? I think that it would need to deal with cases like explained above where the activity could be closed/destroyed before the task completes. This may mean that the task can be cancelled or the task could run but not update the UI because it no longer exists.

In my case the requests that I make are within different functions doing different things so the callbacks to update the UI are not always the same functions. Also the functions that I am using to make the requests have different parameters. I am not sure if these points make it harder/easier to create a solution to this problem but I presume that I will need to rework a lot of my code.

回答1:

a) Use asynchronous methods to make your requests so that you're not hanging threadpool threads.

b) Increase the connection limit for the domains you are requesting from.

ServicePointManager.FindServicePoint(uri).ConnectionLimit=10000;