|
You last visited: Today at 14:17
Advertisement
[Src]ActionQueue
Discussion on [Src]ActionQueue within the CO2 PServer Guides & Releases forum part of the CO2 Private Server category.
02/05/2012, 02:51
|
#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.
|
|
|
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.
|
|
|
02/05/2012, 12:15
|
#3
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
|
Why not just use Tasks?
|
|
|
02/05/2012, 13:32
|
#4
|
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
|
Quote:
Originally Posted by Korvacs
Why not just use Tasks?
|
Tasks? Could you be more specific?
|
|
|
02/05/2012, 16:16
|
#5
|
elite*gold: 0
Join Date: Jun 2005
Posts: 692
Received Thanks: 353
|
Quote:
Originally Posted by I don't have a username
Tasks? Could you be more specific?
|
|
|
|
02/05/2012, 16:31
|
#6
|
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
|
Quote:
Originally Posted by nTL3fTy
|
Didn't know about that ;o Will look up on it. Thanks.
|
|
|
02/05/2012, 16:33
|
#7
|
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
|
|
|
02/05/2012, 16:36
|
#8
|
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
|
Quote:
Originally Posted by Korvacs
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
|
#9
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
|
Quote:
Originally Posted by I don't have a username
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
|
#10
|
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
|
Quote:
Originally Posted by Korvacs
^
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
|
#11
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
|
Quote:
Originally Posted by I don't have a username
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.
|
|
|
02/07/2012, 17:44
|
#12
|
elite*gold: 0
Join Date: Jul 2011
Posts: 218
Received Thanks: 33
|
Quote:
Originally Posted by Korvacs
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.
|
|
|
02/07/2012, 17:48
|
#13
|
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
|
Quote:
Originally Posted by 12k
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
|
|
|
02/07/2012, 19:14
|
#14
|
elite*gold: 0
Join Date: Jul 2011
Posts: 218
Received Thanks: 33
|
Quote:
Originally Posted by Korvacs
I do it as a hobby
|
a very obsessive hobby
|
|
|
02/07/2012, 19:33
|
#15
|
elite*gold: 12
Join Date: Jul 2011
Posts: 8,211
Received Thanks: 4,114
|
Quote:
Originally Posted by blaсkNull
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;
}
}
|
|
|
All times are GMT +2. The time now is 14:17.
|
|