Register for your free account! | Forgot your password?

You last visited: Today at 14:17

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Src]ActionQueue

Discussion on [Src]ActionQueue within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
[Src]ActionQueue

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:


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.
I don't have a username is offline  
Old 02/05/2012, 07:06   #2
 
elite*gold: 0
Join Date: Jan 2012
Posts: 21
Received Thanks: 7
I think you stole this from Fang.
blaсkNull is offline  
Old 02/05/2012, 12:15   #3


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
Why not just use Tasks?
Korvacs is offline  
Old 02/05/2012, 13:32   #4
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by Korvacs View Post
Why not just use Tasks?
Tasks? Could you be more specific?
I don't have a username is offline  
Old 02/05/2012, 16:16   #5
 
nTL3fTy's Avatar
 
elite*gold: 0
Join Date: Jun 2005
Posts: 692
Received Thanks: 353
Quote:
Originally Posted by I don't have a username View Post
Tasks? Could you be more specific?
nTL3fTy is offline  
Thanks
2 Users
Old 02/05/2012, 16:31   #6
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Quote:
Originally Posted by nTL3fTy View Post
Didn't know about that ;o Will look up on it. Thanks.
I don't have a username is offline  
Old 02/05/2012, 16:33   #7


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
Why does no one know about modern programming methods o.O
Korvacs is offline  
Thanks
1 User
Old 02/05/2012, 16:36   #8
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
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.
I don't have a username is offline  
Old 02/05/2012, 16:42   #9


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
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.
Korvacs is offline  
Thanks
2 Users
Old 02/05/2012, 16:48   #10
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
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
I don't have a username is offline  
Old 02/05/2012, 16:49   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
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 **** exists and then save yourself the time of trying to find something, or writing something that already exists.
Korvacs is offline  
Thanks
1 User
Old 02/07/2012, 17:44   #12
 
12k's Avatar
 
elite*gold: 0
Join Date: Jul 2011
Posts: 218
Received Thanks: 33
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 **** 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.
12k is offline  
Old 02/07/2012, 17:48   #13


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
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
Korvacs is offline  
Old 02/07/2012, 19:14   #14
 
12k's Avatar
 
elite*gold: 0
Join Date: Jul 2011
Posts: 218
Received Thanks: 33
Quote:
Originally Posted by Korvacs View Post
I do it as a hobby
a very obsessive hobby
12k is offline  
Old 02/07/2012, 19:33   #15
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,211
Received Thanks: 4,114
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;
        }
    }
Spirited is offline  
Reply




All times are GMT +2. The time now is 14:17.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.