Good day to all!

Purpose: having received the list of participants in the Vkontakte conversation, I listbox trying to display in the new stream using their unique identifier the listbox and listbox Name in the listbox .

Synchronously, everything turns out, but you know that while the program goes to sleep for a couple of seconds. First, it is necessary that the form be initialized, appear on the screen, and then task.Start(); is launched task.Start(); ... but alas, it does not list anything.

Here is the method, and running the method in an asynchronous stream:

 private void GetUsersOnGroup() { // Получить Id всех участников беседы var users = api.Messages.GetChatUsers(chatId); // Максимальное значение - это кол-во элементов в коллекции pb_Process.Maximum = users.Count; foreach (var userId in users) { // Добавить в ListBox новую "Фамилия И." участника беседы lb_Users.Items.Add(api.Users.Get(userId).LastName + " " + api.Users.Get(userId).FirstName[0] + "."); pb_Process.Value++; // Увеличить значение прогресс бара на 1 } } 

The following part of the code is written to the end of the main form constructor:

 Task task = new Task(GetUsersOnGroup); task.Start(); 
  • For me, multithreading is a very sore subject. I really need advice. - Kryshtop
  • Dabbling, made the method asynchronous async, and paused 50 milliseconds before adding a line to the listbox using await Task.Delay (50); - Kryshtop
  • I'm using wpf - Kryshtop

1 answer 1

In general, the people, I made a mistake, did not agree that I took the collection from the server vk.com, and searched for users by id is also not local :) I realized that I needed a certain timeout allocated to each request,

ask.Delay(150); // Highlighting 150 milliseconds for each search query by name, I boldly filled the list in the new stream. And yet, if there is a better way - write in here in the comments. Thank you very much in advance.