Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当ConcurrentQueue包含太多项时工作线程阻塞_C#_Multithreading_Winforms_Thread Safety_Concurrent Queue - Fatal编程技术网

C# 当ConcurrentQueue包含太多项时工作线程阻塞

C# 当ConcurrentQueue包含太多项时工作线程阻塞,c#,multithreading,winforms,thread-safety,concurrent-queue,C#,Multithreading,Winforms,Thread Safety,Concurrent Queue,这是一个奇怪的问题,我有一个Thread[]工作线程,每个工作线程在ConcurrentQueue中处理项目,直到队列为空,程序的其余部分在此继续 这会一直工作到大约1500个项目,此时所有线程都会在WaitSleepJoin状态下保持阻塞状态,并且从不处理队列中的任何项目 我尝试过单步执行我的代码,但线程似乎仍然被创建、启动并处于活动状态,但立即被阻止,并且从未运行过相关的函数 我完全不知所措,所以任何帮助都将不胜感激 守则的有关章节如下: 主螺纹段: Concurre

这是一个奇怪的问题,我有一个
Thread[]
工作线程,每个工作线程在
ConcurrentQueue
中处理项目,直到队列为空,程序的其余部分在此继续

这会一直工作到大约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);
            }
令人困惑的部分似乎是,这一切是如何围绕队列中的项目数展开的,但似乎没有溢出

更新:添加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();
    }
}
关于限制运行进程的数量等,在后台还有更多的工作要做,但是我发现,偶尔,由于未知的原因,我会在任务管理器中看到几个进程永远挂在那里


希望这能有所帮助?

您完全无法使用应用程序。我不确定如果没有air,它还能存活多久。添加了1500个文件,以前它不会处理任何文件blocking@HenkHolterman这应该不会花费超过20秒的时间来运行,并且只是一个内部工具,所以如果windows决定它没有响应,并且您确定没有处理任何内容,我们不会大惊小怪?(不仅仅是Progressbar错误)。它挂在哪里?While循环正在运行吗?设置
useshellexecutte=false
似乎可以实现这一点,不知道为什么会根据帮助的文件数来确定这一点,谢谢。但不知道为什么它会挂起来。
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");
}
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();
    }
}