• There is a class representing a certain API functionality. In the class code, there is a counter that changes after each call to one of the API methods. The counter determines the number of items received when calling the API method and is used for "internal consumption".
  • If you call methods from different threads, the value of the counter will not be relevant.
  • There is an idea to implement the counter as a dictionary.

Details There is an API for VK . I compile from modified sources. The essence of the changes is that each method call that receives a list of something, for example a list of message objects, from the json response, stores the TOTAL number of all messages in the counter. It is necessary for the self-written methods of the GetAll * type to work, which call the original API method several times, with the necessary offset, since Contact API does not allow you to get everything in one request. The counter is needed to determine the required number of calls.

But there is one moment. I would like to leave the possibility to get the value of this counter outside the API methods. The only alternative I can see is adding a variable like "out int totalCount" to each method of the original API, which is not good.


How to implement it correctly?
Or perhaps there is another, more successful way to solve such problems?

  • one
    You can use ThreadStatic or ThreadLocal <T> . - PetSerAl
  • 2
    I advise you to read: ru.stackoverflow.com/questions/420841/… - Alexis
  • one
    @ Alexey Efremov is almost always a different, more successful way to solve such problems than using ThreadStatic / ThreadLocal, but this answer cannot be given until there is an explanation of how this counter is used. - Pavel Hritonenko
  • one
    You don't need ThreadStatic fields for this. It is required to save this counter not on the “stream”, but on the “iteration”. This counter can be encapsulated in a local method variable that returns IEnumerable<T> - Pavel Hritonenko
  • one
    @ Alexey Efremov transfer your explanation to the body of the question - it completely changes the essence. - PashaPash ♦

1 answer 1

For this case, ThreadStatic fields are not needed. It is required to save this counter not on the “stream”, but on the “iteration”. This counter can be encapsulated in a local method variable that returns an IEnumerable<T>

The code below is not working, or even compiled, but it can be understood from what I meant by my answer.

 public static IEnumerable<User> GetAllFollowers(long userId) { const int pageSize = 50; var startRow = 0; while (true) { var page = _vkApi.GetFollowers(userId, startRow, pageSize); if (page.Length != 0) { foreach(var user in page) yield user; startRow += page.Length; } else break; } } 
  • If it's not hard for you, move the whole explanation (and comb it into a coherent answer), okay? Usually no one looks in a comment, so the value of a response based on a comment is low. - VladD
  • @VladD will do as soon as the original message is corrected so as not to play the damaged phone so that the answer is to the question asked. - Pavel Hritonenko
  • Fixed (just now). - VladD
  • @PavelHritonenko, there is one moment. I would like to leave the possibility to get the value of this counter outside the API methods. The only alternative I can see is adding a variable like "out int totalCount" to each method of the original API, which is not good. - Alexey Efremov
  • one
    @ AlekseyEfremov I'm not talking about what needs to be done "out totalCount", but about intentions - why it is needed, how it is planned to use this value, for what purposes. Each of the original methods already returns a collection that has a size. PS Edit code 3rd party libraries - very bad. - Pavel Hritonenko