[Src]ActionQueue

02/05/2012 02:51 I don't have a username#1
A really simple multithread wrapper with a queue of objects to execute.

Features:
Quote:
-Multithreading
-Execute a queue of objects
-Never a duplicate execution
-Simple and fast
Download:
[Only registered and activated users can see links. Click Here To Register...]

Example use: (Also included in the download.)
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using QueLib.QueueThreading;

namespace ConsoleApplication1
{
    public sealed class Player : ActionQueueObject
    {
        public Player(string Name)
            : base()
        {
            this.Name = Name;
            this._Event = new ActionQueueEvent(ActionQueueExecute);
        }

        public string Name;
        private ActionQueueEvent _Event;
        public override ActionQueueEvent Event
        {
            get
            {
                return _Event;
            }
        }
        public void ActionQueueExecute()
        {
            Console.WriteLine("Something to execute {0}", Name);
            FinishQueueState();
        }
    }
    public sealed class PlayerThread : ActionQueue
    {
        public PlayerThread(int Interval)
            : base(Interval)
        {
        }
        public Dictionary<uint, Player> Players = new Dictionary<uint, Player>();

        public override ActionQueueObject[] queueObjects
        {
            get
            {
                return Players.Values.ToArray();
            }
        }
    }
    
    class Program
    {
        static PlayerThread PlayerThread;

        static void Main(string[] args)
        {
            PlayerThread = new PlayerThread(5000);
            PlayerThread.Players.Add(0, new Player("LOL"));
            PlayerThread.Prepare();
            PlayerThread.Start();

            while (true)
                Console.Read();
        }
    }
}
Good luck.

Note:
FinishQueueState(); has to be called in the end of the execution no matter what. It will reset the queuestate, so it will be executed by the thread next time.
02/05/2012 07:06 blaсkNull#2
I think you stole this from Fang.
02/05/2012 12:15 Korvacs#3
Why not just use Tasks?
02/05/2012 13:32 I don't have a username#4
Quote:
Originally Posted by Korvacs View Post
Why not just use Tasks?
Tasks? Could you be more specific? :)
02/05/2012 16:16 nTL3fTy#5
Quote:
Originally Posted by I don't have a username View Post
Tasks? Could you be more specific? :)
[Only registered and activated users can see links. Click Here To Register...]
02/05/2012 16:31 I don't have a username#6
Quote:
Originally Posted by nTL3fTy View Post
[Only registered and activated users can see links. Click Here To Register...]
Didn't know about that ;o Will look up on it. Thanks.
02/05/2012 16:33 Korvacs#7
Why does no one know about modern programming methods o.O
02/05/2012 16:36 I don't have a username#8
Quote:
Originally Posted by Korvacs View Post
Why does no one know about modern programming methods o.O
I never really look up much on things, unless is something I really need or want to learn about.
02/05/2012 16:42 Korvacs#9
Quote:
Originally Posted by I don't have a username View Post
I never really look up much on things, unless is something I really need or want to learn about.
^

The problem with this generation of developers.
02/05/2012 16:48 I don't have a username#10
Quote:
Originally Posted by Korvacs View Post
^

The problem with this generation of developers.
I don't have a lot free time on my hands, so can't use my time researching every single way to do things best :P
02/05/2012 16:49 Korvacs#11
Quote:
Originally Posted by I don't have a username View Post
I don't have a lot free time on my hands, so can't use my time researching every single way to do things best :P
Thats fine, but you could spend 5 minutes reading the release notes for the .net framework updates so that you know that this shit exists and then save yourself the time of trying to find something, or writing something that already exists.
02/07/2012 17:44 12k#12
Quote:
Originally Posted by Korvacs View Post
Thats fine, but you could spend 5 minutes reading the release notes for the .net framework updates so that you know that this shit exists and then save yourself the time of trying to find something, or writing something that already exists.
Some people do it as a hobby, not as a living. Might be part of the reason they don't keep up with every little thing with it.
02/07/2012 17:48 Korvacs#13
Quote:
Originally Posted by 12k View Post
Some people do it as a hobby, not as a living. Might be part of the reason they don't keep up with every little thing with it.
I do it as a hobby :rolleyes:
02/07/2012 19:14 12k#14
Quote:
Originally Posted by Korvacs View Post
I do it as a hobby :rolleyes:
a very obsessive hobby :D
02/07/2012 19:33 Spirited#15
Quote:
Originally Posted by blaсkNull View Post
I think you stole this from Fang.
Nah. This is mine:
Code:
    public sealed class ThreadedQueue
    {
        // Configuration:
        private readonly object _locker;
        private Thread[] _workers;
        private Queue<Action> _queue;

        public ThreadedQueue(int workerCount)
        {
            _locker = new object();
            _queue = new Queue<Action>();
            _workers = new Thread[workerCount];
            for (int i = 0; i < workerCount; i++)
                (_workers[i] = new Thread(Dequeue)).Start();
        }
        private void Dequeue()
        {
            while (true)
            {
                Action obj;
                lock (_locker)
                {
                    while (_queue.Count == 0) 
                        Monitor.Wait(_locker);
                    obj = _queue.Dequeue();
                }
                if (obj == null)
                    return;
                obj();
            }
        }
        public void Enqueue(Action method)
        {
            lock (_locker)
            {
                _queue.Enqueue(method);
                Monitor.Pulse(_locker);
            }
        }
        public void Shutdown(bool waitForWorkers)
        {
            foreach (Thread worker in _workers)
                Enqueue(null);
            if (waitForWorkers)
                foreach (Thread worker in _workers)
                    worker.Join();
            _workers = null;
            _queue = null;
        }
    }