wiz0u / WTelegramClient

Telegram Client API (MTProto) library written 100% in C# and .NET
https://wiz0u.github.io/WTelegramClient/
MIT License
956 stars 156 forks source link

CollectUsersChats: error Collection was modified; enumeration operation may not execute. #266

Closed devtunnelx closed 2 months ago

devtunnelx commented 2 months ago

Version: 4.1.2-dev.7

I'm having an issue when using UpdateManager.CollectUsersChats()

Exception keeps thrown after running the app (like 30 minutes or 45 later)

Collection was modified; enumeration operation may not execute.

Current code:

  var users = new Dictionary<long, User>();
  var chats = new Dictionary<long, ChatBase>();

                lock (_updatesManagerLock)
                {
                    var manager = _updateManagers[client];
                    manager.CollectUsersChats(users, chats);

                }

Even when using _lock and thread safety masques, i still get the error at manager.CollectUsersChats(users, chats);

wiz0u commented 2 months ago

What are you trying to achieve exactly?

You're not supposed to call manager.CollectUsersChats

devtunnelx commented 2 months ago

What are you trying to achieve exactly?

You're not supposed to call manager.CollectUsersChats

Basically, my app listen to incoming Telegram updates and I want to get the sender and chat information from the update then enqueue it to be processed by queue consumers.

If using manager.CollectUsersChats is not the right way, what would you suggest?

thank you sir

wiz0u commented 2 months ago

Use Manager.UserOrChat or Manager.Users/Chats dictionaries to resolve peers & ids into details

or call structure.CollectUsersChats(Manager.Users, Manager.Chats); after an API call that return a structure which contains users/chats

As demonstrated in https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ListenUpdates.cs

See also these explanations: