c# - C# 中奇怪的循环

标签 c# for-loop openfiledialog

注意:我添加了实际的代码片段。只需滚动到末尾。

// files is created by a OpenFileDialog.
public void Function(String[] files, ...)
{
    for(int i; i<files.Length; i++)
    {
        WriteLine("File " + i + "/" + files.Length + " being processed.");
        //... processing for a long time and printing information to console ...
    }

    //... print results, e.g.: "Results: bla bla"...
}

函数在另一个循环中被调用。我运行了几次代码并认为它​​是 工作得很好,直到我看到有一次它表现得很奇怪。我提供了以上功能 数组长度为 6,预期输出如下:

-------------------------
File 0/6 being processed.
...lots of output...
File 1/6 being processed.
...lots of output...
File 2/6 being processed.
...lots of output...
File 3/6 being processed.
...lots of output...
File 4/6 being processed.
...lots of output...
File 5/6 being processed.
...lots of output...

Results: bla bla...
-------------------------

然而,我得到的输出是这样的:

-------------------------
File 0/1 being processed.
...lots of output...

Results: bla bla...

File 0/3 being processed.
...lots of output...
File 1/3 being processed.
...lots of output...
File 2/3 being processed.
...lots of output...

Results: bla bla...

File 0/3 being processed.
...lots of output...
File 1/3 being processed.
...lots of output...
File 2/3 being processed.
...lots of output...

Results: bla bla...

File 0/6 being processed.
...lots of output...
File 1/6 being processed.
...lots of output...
File 2/6 being processed.
...lots of output...
File 3/6 being processed.
...lots of output...
-------------------------

当我看到那个输出时,我在当前循环结束之前退出了执行(它运行 很长一段时间。)

看起来该函数正常工作(它运行 files.Length 次并在之后输出结果。)但是,传递给该函数的参数在某种程度上是错误的(有趣的是,该函数被调用了不止一次。通常,它应该在这种情况下只运行一次。我的意思是,脚本文件中的行数决定了调用上述函数的次数,并且脚本文件只包含一行。)该参数(文件数组)来自 OpenFileDialog ,这意味着我与它无关。我只是将数组传递给函数。

我仍在努力理解出现这种奇怪结果的原因。这只发生过一次,但我仍然需要诊断问题;因为,我可能会让程序运行几天。它应该可以正常工作。

你对这些废话有什么想法吗?


上述函数的实际代码:

public String Start(String[] files, StreamWriter reportWriter)
{
    List<SortedDictionary<int, SortedDictionary<long, int>>>[] allResults
        = new List<SortedDictionary<int,SortedDictionary<long,int>>>[files.Length];
    List<SortedDictionary<int, SortedDictionary<long, int>>> results;
    Simulation_DenemePositionEstimator p;
    Simulation_WimaxStreamReader reader;
    String ret;

    for (int i = 0; i < files.Length; i++)
    {
        System.Console.WriteLine("File " + (i+1) + "/" + files.Length + " being processed.");
        reader = new Simulation_WimaxStreamReader(grids, new StreamReader(files[i]));
        p = new Simulation_DenemePositionEstimator(grids, reader);
        // Using parameters in script file which were saved into
        // different variables when Simulation instance was created.
        results = 
            p.StartInvestigation(maxRssiDiff, maxCinrDiff, maxAvgTxPwrDiff, 
                maxUncontinuity, radiusForNeighbors, expansionFactor, increment,
                n, numberOfIterations, resetCountForPositioning);
        allResults[i] = results;
        reader.Close();
    }

    ret = Statistics(allResults);
    System.Console.WriteLine(ret);
    reportWriter.WriteLine(ret);
    reportWriter.Flush();

    return ret;
}

调用函数代码:

    // read a line from script file.
    while((line = reader.ReadLine()) != null)
    {
        // line starting with # is comment.
        if (line.StartsWith("#") == false)
        {
            // save parameters retrieved from script file into an array.
            values = line.Split(delimiters);
            // new Simulation instance with new parameters
            sim = new Simulation(map, values);
            // Start simulation. scenarioFiles comes from OpenFileDialog.
            report = sim.Start(scenarioFiles, reportWriter);
            //reportWriter.WriteLine(report);
            reportWriter.WriteLine("---------------NEW-PARAMETERS---------------");
            reportWriter.Flush();
        }
    }

脚本文件:

# Horizontal grid count
# Vertical grid count
# maxRssiDiff is the maximum RSSI difference allowed.
# maxCinrDiff is the maximum CINR difference allowed.
# maxAvgTxPwrDiff is the maximum AvgTxPwr difference allowed.
# maxUncontinuity
# radiusForNeighbors
# expansionFactor
# increment
# n -> MT'den gelen kaç değerin ortalaması alınıp yer bulma algoritmasına girdi olarak verilsin?
# Algoritma kaç adımda bir sonuçları dosyaya yazsın?
# Kaç adımdan sonra yer bulma işlemine sıfırdan başlamış gibi devam etsin?
# 
# Örnek:
# 118   90  4   3   4   2   1   1   1   3   10  100
118 90  6   4   6   2   1   1   1   3   250 500
# 200   140 4   3   4   2   1   1   1   3   10  100

最佳答案

似乎有什么比您预期的更频繁地调用该方法。

在该方法的第一行放置一个断点,并查看它何时以及为何被调用。该错误几乎必然存在于调用代码中,而不是方法本身,这意味着我们只能提供断点、记录堆栈跟踪等建议。

关于c# - C# 中奇怪的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1734176/

相关文章:

c# - DocumentDb DocumentClient 获取前 25 个文档

c++ - 如何创建一个 vector 并以相反的顺序输出

c - 在 c 中输入无效后重新提示用户

python - Windows 10 资源管理器文件打开对话框 : filenames disappearing in compiled executable

c# - 在 Entity Framework 中的抽象类上配置自动增量字段

c# - 产品多时的生产者消费者模式

c# - 如何在 C# 中更快地计算简单移动平均线?

python - 以更简单的方式调用不同的函数

c# - 在打开文件对话框之前显示图片框中的默认图片

c++ - 如何设置 Windows 文件打开对话框过滤器以包含 'must finish with' 选择