This project has moved and is read-only. For the latest updates, please go here.
1
Vote

Any way to make the async callbacks in ATD thread-safe?

description

Hey!

I've been using your code for a bit now, and I've come to realize that my code is not very thread-safe at all, most likely thanks to my amateur programming. :-)

I'm thinking of building some sort of Mutex-like code in the events that are invoked from your AsyncTcpDispatcher (like when people move channels etc), but as I still consider myself an amateur, I thought I'd ask for your advice on this.

Basically, I want my events for when clients change channels to properly queue up and not fire away all at once, causing my instanced code to run multiple times from different callbacks (= non-threadsafe stuff).

If you have any quick or simple tips to get these callbacks properly in line, I'd greatly appreciate them!

Best regards.
Will

comments

qvazzler wrote May 28, 2014 at 11:18 PM

By the way, this is how I'm doing it currently. Not sure if it's the best way to go but it seems to have simple potential, using SemaphoreSlim.
SemaphoreSlim sema;
sema = new SemaphoreSlim(1);
        private void Notifications_ClientMoved(object sender, TS3QueryLib.Core.Server.Notification.EventArgs.ClientMovedEventArgs e)
        {
            sema.Wait();

            doubleEvent = !doubleEvent;
            if (doubleEvent)
            {
                Console.WriteLine("Callback!");
                Thread.Sleep(5000);
                //TidyChannels((int)e.TargetChannelId, (int)e.ClientId);
            }

            sema.Release();
        }