ConcurrentQueue 异步多线程顺序执行

C# 多线程高并发的情况下,怎么让数据先到先执行?

using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

ConcurrentQueue<string> _queue = new ConcurrentQueue<string>();
......
for (int indx = 1; indx <= 3; indx++)
{
    string command = $"build.bat \"{_curDir}\" \"{_versionInfo}\" \"{_args3[indx]}\" \"{_args4[indx]}\" \"{_args5[indx]}\"";
    _queue.Enqueue(command);
}
......
while (_queue.TryDequeue(out string cmd))
{
    btnBuild.Dispatcher.Invoke(() =>
    {
        btnBuild.IsEnabled = false;
    });
    Thread thread = new Thread(Worker);
    thread.IsBackground = true;
    thread.Start(cmd);
}
......
private void Worker(object command)
{
    Thread.Sleep(100);
    // do same sth
}
posted @ 2019-11-20 10:13  wesson2019  阅读(565)  评论(0编辑  收藏  举报