c# - 当 ConcurrentQueue 有太多项目时工作线程阻塞

标签 c# multithreading winforms thread-safety concurrent-queue

这很奇怪,我有一个 Thread[]每个工作线程处理 ConcurrentQueue<string> 中的项目直到队列为空,此时程序的其余部分继续。

这一直有效到大约 1500 个项目,此时所有线程都在 WaitSleepJoin 中保持阻塞状态状态并且从不处理队列中的任何项目。

我已经尝试单步执行我的代码,看起来线程仍在创建、仍在启动并且处于事件状态,但立即被阻止并且永远不会运行它们的相关功能。

我完全糊涂了,所以任何帮助将不胜感激!

相关部分代码如下:

主线程段:

            ConcurrentQueue<string> convertionQueue = new ConcurrentQueue<string>();
            List<Thread> converterThreads = new List<Thread>();
            Directory.GetFiles(_folderOne, "*.fdf", SearchOption.AllDirectories).ToList().ForEach(file => convertionQueue.Enqueue(file));
            Directory.GetFiles(_folderTwo, "*.fdf", SearchOption.AllDirectories).ToList().ForEach(file => convertionQueue.Enqueue(file));
            int filesDone = 0;
            int totalFiles = convertionQueue.Count;
            progressBar.Maximum = totalFiles;
            panel1.Visible = true;
            for (int i = 0; i < Environment.ProcessorCount; i++)
            {
                converterThreads.Add(new Thread(() => ConvThreadWorker(convertionQueue, ref filesDone)));
            }
            converterThreads.ForEach(thread => thread.Start());
            DateTime lastTick = DateTime.Now;
            int lastFilesDone = 0;
            int[] valuesSpeed = { 1, 1, 1, 1, 1 };
            int[] valuesTime = { 1, 1, 1, 1, 1 };
            int counter = 0;
            while (converterThreads.Any(thread => thread.IsAlive))
            {

                TimeSpan t = DateTime.Now - lastTick;
                int deltaFiles = filesDone - lastFilesDone;
                double speed = (float)t.TotalMilliseconds <= 0.0 ? 0.0 : deltaFiles / (float)t.TotalMilliseconds;
                double tMinus = speed <= 0 ? 0.0 : (totalFiles - filesDone) / speed;
                int currentSpeed = (int)(speed * 1000);
                int currentTime = (int)(tMinus / 1000);
                valuesSpeed[counter] = currentSpeed;
                valuesTime[counter] = currentTime;
                lblFilesLeft.Text = string.Format("{0}/{1}", filesDone, totalFiles);
                lblSpeed.Text = valuesSpeed.Sum() / 5 + " /s";
                lblTime.Text = valuesTime.Sum() / 5 + " s";
                lblFilesLeft.Update();
                lblSpeed.Update();
                lblTime.Update();
                progressBar.Value = filesDone;
                progressBar.Update();
                lastTick = DateTime.Now;
                lastFilesDone = filesDone;
                counter = ++counter % 5;
                Thread.Sleep(500);
            }

worker 函数:

private void ConvThreadWorker(ConcurrentQueue<string> queue, ref int fileCounter)
{
    while (!queue.IsEmpty)
    {
        string file;
        if (queue.TryDequeue(out file))
        {
            ConvToG(file);
            fileCounter++;
        }
    }
}

转换函数:

private void ConvToG(string file)
{
    MessageBox.Show("Entering Convertion Function");
    if (!_fileCreationDictionary.ContainsKey(file))
    {
        DateTime lastTimeModified = File.GetLastWriteTime(file);
       _fileCreationDictionary.AddOrUpdate(file, lastTimeModified, (key,oldvalue)=>lastTimeModified);
    }
    ProcessStartInfo procStart = new ProcessStartInfo
    {
        Arguments = file,
        UseShellExecute = true,
        FileName = Fdfg,
        WindowStyle = ProcessWindowStyle.Hidden
    };
    Process process = new Process {StartInfo = procStart};
    MessageBox.Show("Starting convertion process");
    process.Start();
    process.WaitForExit();
    MessageBox.Show("Finished");
}

令人困惑的部分似乎是这一切如何围绕队列中的项目数量进行,但似乎没有溢出。

更新:添加 mbox 显示它在 process.Start() 上卡住一段代码,没有错误,不会继续超过该点。

更新 2:如果 UseShellExecute = false代码有效。这至少可以说是非常困惑的。

最佳答案

我用线程生成进程做了类似的事情来整理数据。我在实际过程开始和挂起时遇到了问题。我为使我的程序正常工作所做的事情是这样的:

using (Process process = Process.Start(startInfo)) {
    if(process.WaitForExit(timeOutMilliseconds)) {
        MessageBox.Show("Process exited ok");
        //...snip
    } else {
        MessageBox.Show("Process did not exit in time!");
        //...snip
        process.Kill();
    }
}

后台还有更多操作,比如限制正在运行的进程的数量等,但我发现偶尔,出于未知原因,我会在任务管理器中看到几个进程永远挂着。

希望对您有所帮助?

关于c# - 当 ConcurrentQueue 有太多项目时工作线程阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489112/

相关文章:

c# - 如何在 Azure 移动应用上托管 API

java - java中有 'block until condition becomes true'函数吗?

c++ - 是否将 unique_lock 用于可以由 lock_guard 完成的任务较慢?

C# - 在 TextBox 上使用异步和等待的跨线程错误

c# - 为什么 NumberFormatInfo 在 Azure 环境中的行为不同?

c# - Xamarin 动态加载本地化文件

c# - 是否存在可以在编译的 IL 中以某种方式引用属性的情况?

java - while 循环停止检查 if 语句?

c# - .Net : Arranging Many Windows Forms on Screens

winforms - 最佳实践 : Blocking execution while waiting for an Async method