I want to make a queue manager which execute tasks(method) in ConcurrentQueue. The function of the queue manager is executing any tasks(methods) in queue automatically. to do this, I try to pass task(method) into queue, and try to execute that task(method) automatically by dequeueing.
This is main program
public partial class TraderMain : Form
{
.
.
.
public static QueueManager _queueMgr ;
.
.
.
//it return instance and if it doesn't exist, make a instance
_queueMgr = SingletonBase<QueueManager>.Instance;
//start dequeueing
Task dequeueTask = Task.Factory.StartNew( () => { _queueMgr.Dequeue(); } );
.
.
.
BulkDataLoad bdl = new BulkDataLoad();
Task enqueueTask = Task.Factory.StartNew(() => bdl.sendRequestAllItemsInfo());
below is the BulkDataLoad's source.
public int sendRequestAllItemsInfo()
{
foreach (CodeManager.MasterCode masterCode in lMasterCodes)
{
// enqueueing Tasks(method)
TraderMain._queueMgr.Enqueue(() => ActionWithCode(masterCode.Code));
I expect that the method in queue will be performed automatically by dequeueing. when I execute that program, the work is not work perfectly.
the enqueueing works well. but, dequeueing deesn't work. when I debug the program, the debugger doesn't go into the method "ActionWithCode".
what I want is to pass task as a parameter to be execute automatically. is it not possible? if then, how should I modify the source? below is the QueueManager class source.
public class QueueManager
{
static readonly int REQUEST_DELAY = 10;
public ConcurrentQueue<Task> queueTR;
private QueueManager()
{
queueTR = new ConcurrentQueue<Task>();
}
public void Enqueue(Action action, CancellationToken cancelToken = default(CancellationToken))
{
Task task = new Task(action);
queueTR.Enqueue(task);
}
public void Dequeue()
{
while (true)
{
try
{
if (queueTR.TryDequeue(out Task result)) { Debug.WriteLine("> Queue : " + result); }
Thread.Sleep(REQUEST_DELAY);
}
catch (Exception ex)
{
}
}
}
}