0

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)
                {
                }
            }
        }
    }
SW J
  • 1
  • "dequeueing deesn't work" - can you elaborate? If you haven't realized `Task.Factory.StartNew` starts executing the task, so when you dequeue it from the `QueueManager` it may have already finished running. Please also take a look at the [remarks for `TaskFactory.StartNew`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskfactory.startnew?view=net-5.0#remarks) - you might want to use `Task.Run` instead. – Xerillio Mar 23 '21 at 19:37
  • It seems not to be done. because debugger does not step into the method. – SW J Mar 24 '21 at 23:43
  • I got it. after dequeueing, I add `result.RunSynchronously();` and it does work well. – SW J Mar 25 '21 at 05:09

0 Answers0