C# 中奇怪的循环

发布于 2024-08-11 00:36:31 字数 4780 浏览 2 评论 0原文

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

// 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

Note: I added actual code snippets. Just scroll to end.

// 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"...
}

Function is called in another loop. I ran the code a few times and thought it was
working well until I saw that one time it acted weird. I provided the above function
with an array length of which was 6, and the expected output was like this:

-------------------------
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...
-------------------------

However, the output I got was like that:

-------------------------
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...
-------------------------

When I saw that output I quit the execution before the current loop was over (it runs
for a very long time.)

It looks like the function is working correctly (It runs files.Length times and outputs results after that.) However, the argument passed to the function is somehow faulty (The function is interestingly called more than once. Normally, it should run for only one time in this case. I mean, the number of lines in a script file determine the number of times above function is called, and the script file contains only one line.) That argument (files array) comes from a OpenFileDialog, which means I have nothing to do with it. I just pass the array to the function.

I'm still trying to understand the reason for such a strange outcome. This only happened one time, but I still have to diagnose the problem; because, I will leave the program running maybe for a couple of days. It should work correctly.

Do you have any ideas about this nonsense?


Actual code of above function:

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;
}

Caller function code:

    // 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();
        }
    }

Script file:

# 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2024-08-18 00:36:31

似乎某些东西调用该方法的频率比您预期的要高。

在方法的第一行放置一个断点,然后查看调用它的时间和原因。该错误几乎肯定存在于调用代码中,而不是方法本身,这意味着我们除了建议断点、记录堆栈跟踪等之外,无能为力。

It seems like something is calling the method more often than you expect.

Put a breakpoint on the first line of the method, and see when and why it's being called. The bug is almost bound to be in the calling code rather than the method itself, which means we can't really help much more than suggesting things like breakpoints, logging stack traces etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文