1

感谢您查看我的问题:我有一个(非 gui 线程)BlockingCollection,我一直认为它是 FIFO(先进先出),但我现在意识到它不是

我在dotnetfiddle上粘贴了一个代码示例,但是因为它不运行多线程,所以你看不到错误发生,但你可以看到代码

好的。那我想要什么?我想在其中创建第二个线程(非 GUI),Visual Studio Express 2013 C# Winforms就像一个工作室,它会按照发送它们的顺序执行它传递的事情。

我选择了这样的结构:

nofQDo
|_addAction(|)
            |
            +-> static BlockingCollection foreach
                                          |
                                          +-> QDo.run(|)
                                                      |
                                                      +> QDoType.action(//code//)

这种奇怪安排的原因是我想拥有多达 20 或 30types个队列对象(我将这些都称为QDoType_something)并且我对布局感到满意,但如果我调用引擎将无法工作

QDoType_test gra = new QDoType_test("hey0");
nofQDo.addAction(gra);

QDoType_test grb = new QDoType_test("hey1");
nofQDo.addAction(grb);

QDoType_test grc = new QDoType_test("hey2");
nofQDo.addAction(grc);

 QDoType_test grd = new QDoType_test("hey3");
 nofQDo.addAction(grd);

 QDoType_test gre = new QDoType_test("hey4");
 nofQDo.addAction(gre);

 QDoType_test grf = new QDoType_test("hey5");
 nofQDo.addAction(grf);

我明白了

00009::hey0
00009::hey1
00009::hey5
00009::hey3
00009::hey2
00009::hey4

或者

00009::hey1
00009::hey0
00009::hey3
00009::hey2
00009::hey4
00009::hey5

所以它显然不是“ FIFO”,这是令人震惊的..有没有办法确保我BlockingCollection是a)not gui线程b)只作为一个额外的线程运行和c)第二个线程总是在运行FIFO(先进先出?)

根据要求:这是正确的代码:

=QDoType_test.cs=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QTest
{
    class QDoType_test : QDoType
    {
        String szout = "";
        private string ThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString("00000");
        public QDoType_test(String sent)
        {
            szout = sent;
        }
        public override void action()
        {
            System.Threading.Thread.Sleep(100);
            Console.WriteLine(ThreadId + "::" + szout);
        }
    }
}

=nofQDo.cs=

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace QTest
{
    class nofQDo
    {
        static BlockingCollection<QDo> queue = new BlockingCollection<QDo>(new ConcurrentQueue<QDo>()); //<--new ConcurrentQueue<QDo>() makes it FIFO

        public static void addAction(QDoType action)
        {
            QDo me = new QDo(action);
            queue.Add(me);
            Task.Factory.StartNew(() =>
            {
                foreach (QDo doThis in queue.GetConsumingEnumerable())
                {
                    doThis.run();
                }
            });
        }
    }
}

=QDoType.cs=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QTest
{
    /// <summary>
    /// This is a Parent Class for QDoType_whatever they are non 
    /// communicative and most exist to run db calls
    /// </summary>
    public abstract class QDoType
    {
        /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
         * this is a parent class not meant to ever be instaciated   *
        \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

        public string uniqueid = "";
        public Action callback;

        public abstract void action();

        /// <summary>
        /// kept for the fact you might want 
        /// to debug where it went in the Queue
        /// </summary>
        /// <param name="uid"></param>
        public void setUniqueId(string uid)
        {
            uniqueid = uid;
        }
    }
}

=QDo.cs=

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QTest
{
    class QDo
    {
        /***********
         * 
         * This class is the <T> umbrella for a real type that runs inside it
         * basically all this does in "run()" the QDoType;
         */
        public const bool DELETE_MODE = true;

        QDoType iam;

        public QDo(QDoType action)
        {
            DateTime dt = DateTime.Now;
            iam = action;
        }

        public void run()
        {
            iam.action();

            if (iam.callback != null) iam.callback();
        }
    }
}
4

1 回答 1

1

好的,我快到了,它只是确保消费者在生产者之前开始(即在构造函数中),仍然不能 100% 确定为什么会这样(但它确实有效!我向你保证 100%!)它是单次测试的单位也运行。

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace QTest
{
    class nofQDo
    {
        static BlockingCollection<QDo> queue = new BlockingCollection<QDo>(new ConcurrentQueue<QDo>()); //<--new ConcurrentQueue<QDo>() makes it FIFO

        static nofQDo()
        {
            Task.Factory.StartNew(() =>
            {
                foreach (QDo doThis in queue.GetConsumingEnumerable())
                {
                    doThis.run();
                }
            });
        }

        public static void addAction(QDoType action)
        {
            QDo me = new QDo(action);
            queue.Add(me);
        }
    }
}

所以现在

QDoType_test gra = new QDoType_test("hey0"); nofQDo.addAction(gra);

QDoType_test grb = new QDoType_test("hey1"); nofQDo.addAction(grb);

QDoType_test grc = new QDoType_test("hey2"); nofQDo.addAction(grc);

QDoType_test grd = new QDoType_test("hey3");  nofQDo.addAction(grd);

QDoType_test gre = new QDoType_test("hey4");  nofQDo.addAction(gre);

QDoType_test grf = new QDoType_test("hey5");  nofQDo.addAction(grf);

生产

00009::hey0
00009::hey1
00009::hey2
00009::hey3
00009::hey4
00009::hey5
于 2014-07-24T08:57:09.343 回答